delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/12/02:30:35

From: "Steve Patton" <leaphe AT pemail DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Audio recording and mixing
Date: Wed, 11 Feb 1998 22:14:19 -0700
Organization: AT&T WorldNet Services
Lines: 56
Message-ID: <6bu0g2$p5j@bgtnsc02.worldnet.att.net>
References: <34E10439 DOT 17FF AT netvision DOT net DOT il>
NNTP-Posting-Host: 12.67.33.117
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I can't provide answers to the recording part of the question, but as to the
mixing part of the question.  I don't believe averaging the values would
provide correct results, in fact you might get a distorted sound.  Because
sound laws provide that if you have a +1 (doesn't matter what unit) and a +1
wave, you should get a +2, averaging leaves you with a +1.  Adding is the
appropriate way.  EXCEPT, that you are most likely dealing in unsigned
samples.  In that case you might want to try a little hack that I use in my
synthesizer program to mix.

take an int, make it signed

int a;

and then you have two unsigned samples, this can be unsigned short (16-bit)
or unsigned char (8-bit), and do as so

a = (int)(s1 + (s2-128));

the (int) cast is to make sure they get converted to signed int before the
arithmetic.  This is assuming you have 8-bit samples.  We will assume 128 is
neutral (you may want to hack this, since my math may be off by one), and we
subtract 128 from s2, that way, if the value is 128, the end result would be
0 (no change), this is an easy way.  You will have to make certain hacks if
you are mixing a 16 bit to an 8 bit with a 16 bit operation.  But in the
end, just convert "a" to the type of data you need.  If you are using an
8-bit output, you should clip the output if it is outside 0-255, and so
forth.  I use this algorithm in my synthesizer program that mixes on the fly
several sounds at the same time using the AUDIOSTREAM section, and it seems
to mix things rather well.  And I have it in it's own procedure (which is
marked with __inline__ but for testing I don't use -O3 or whatever, so it
isn't inlined, and it is still pretty fast, so you might try it out)

Hope this helps

--
-Steve
http://home.att.net/~pattonl
Guy Rauscher wrote in message <34E10439 DOT 17FF AT netvision DOT net DOT il>...
>Hi!
>
>I'm using Allegro for my audio output and I want to combine recording
>ability in a future application; Any suggestions?
>
>Also, how do I mix to samples? I tried averaging the values of each two
>bytes from the two samples by the volume I got was too low. I also
>tried adding every two bytes but the the values were out of range. What
>do I do?
>
>Alsoly, if anyone had written a modeller before, how do you handle the
>editable range to avoid floating point overflows?
>
>Thanks,
>
>Guy


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019