[Speex-dev] WaveIn/WaveOut and Speex

Evgueni Tsygankov eugenet at rusmex.com
Mon Nov 5 04:27:21 PST 2007


Again, thank you for helping me. I know this might seem like a trivial matter to you and other experts in the field, but believe me, there are a lot of programmers, whose posts I saw on the web, who tried to use WaveIn/WaveOut and Speex and failed.

As I understand it, WaveIn just buffers audio data according to the bit rate specified. So, if we use waveFormat.wBitsPerSample = 8, then each Char of the audio buffer describes an audio sample using 8 bits. On the output side, WaveOut expects each Char to describe an 8-bit audio sample as well. If we deal with 16-bit samples, then two Chars would describe a sample, and so on.

My question is on how to deal with Speex output -- if I get a float or a short from Speex' decode function, how does it map to audio samples described above? 

Thank you for your support,

Evgueni Tsygankov
www.sqlanswers.com
www.rusmex.com


-----Original Message-----
From: Jean-Marc Valin [mailto:jean-marc.valin at usherbrooke.ca] 
Sent: Sunday, November 04, 2007 10:26 PM
To: Evgueni Tsygankov
Cc: speex-dev at xiph.org
Subject: Re: [Speex-dev] WaveIn/WaveOut and Speex

> When I was going from Char to float and back looked very wrong to me

it not only *looked* wrong!

> as well, but I was just not sure (and still am) how to translate the
> Char* audio stream generated by WaveIn to a format that can be
> understood by Speex. Would using speex_decode_int and
> speex_encode_int instead of speex_decode and speex_encode be the
> answer?

Using speex_encode_int() might happen to work on your PC, but then going
to a different platform, endianness might turn around and bite you. The
correct thing would be to encode (either using the int or the float
version) and do the conversion by hand using proper endianness. To do
that, you need to actually figure out how Windows actually interprets
its stream of bytes (can't help you on that).

	Jean-Marc

> Thank you,
> 
> Evgueni Tsygankov www.sqlanswers.com www.rusmex.com
> 
> 
> -----Original Message----- From: Jean-Marc Valin
> [mailto:jean-marc.valin at usherbrooke.ca] Sent: Sunday, November 04,
> 2007 9:33 PM To: Evgueni Tsygankov Cc: speex-dev at xiph.org Subject:
> Re: [Speex-dev] WaveIn/WaveOut and Speex
> 
> I'm not sure what input/output format you're trying to use, but it
> looks wrong. You're using the float functions that take +-32767
> values and you're feeding (or converting) chars. Unless your machine
> has very special chars (which I doubt), a +-32767 value just isn't
> going to fit in. This has nothing to do with Speex BTW, it's just
> handling the audio data properly.
> 
> Jean-Marc
> 
> 
> Evgueni Tsygankov wrote:
>> Hello,
>> 
>> 
>> 
>> I know my question has been asked before because I spent the last
>> week searching the web for how to use Speex in combination with 
>> WaveIn/WaveOut and I ran into a few posts, but none of them answer
>> the question. There is still a lot of confusion how to use
>> WaveIn/WaveOut and Speex by junior developers such as myself. Even
>> after examining code for SpeexDec and SpeexEnc, I cannot get clear
>> sound when I try to
>> 
>> 
>> 
>> Get PCM Char buffer from the microphone
>> 
>> Encode Char buffer using Speex
>> 
>> Decode Speex Char buffer into PCM char buffer
>> 
>> Play PCM char buffer using WaveOut
>> 
>> 
>> 
>> The end-result is very distorted sound.
>> 
>> 
>> 
>> 
>> 
>> I use the following PCM format:
>> 
>> 
>> 
>> waveFormat.nChannels = 1;
>> 
>> waveFormat.nSamplesPerSec = 8000;
>> 
>> waveFormat.wBitsPerSample = 8;
>> 
>> 
>> 
>> Once sound is aquired by the mic, I send it to Speex in a Char
>> buffer. It looks something like this:
>> 
>> 
>> 
>> Encode(char* inBuffer, DWORD bufferLength)
>> 
>> ...
>> 
>> float* input = new float[frameSize];
>> 
>> ZeroMemory(input, frameSize);
>> 
>> ...
>> 
>> for(    )
>> 
>> {
>> 
>> input[..] = inBuffer[..];
>> 
>> }
>> 
>> ...
>> 
>> speex_encode(enc_state, input, &bits);
>> 
>> int bytesWritten = speex_bits_nbytes(&bits);
>> 
>> speex_bits_write(&bits, encBuffer, bytesWritten);
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> Then, my decoding routine accepts the char buffer that was encoded
>> with Speex:
>> 
>> 
>> 
>> Decode(char* encBuffer, DWORD bufferLength)
>> 
>> ...
>> 
>> float* output = new float[frameSize];
>> 
>> ...
>> 
>> speex_bits_read_from(&bits, encBuffer + offset, bytesToRead);
>> 
>> speex_decode(dec_state, &bits, output);
>> 
>> 
>> 
>> char* outBuffer=...
>> 
>> for(UINT i = 0; i < frameSize; i++)
>> 
>> {
>> 
>> outBuffer[retValIndex] = output[i];
>> 
>> retValIndex++;
>> 
>> }
>> 
>> 
>> 
>> When I try to play outBuffer with waveOut, I can hear the voice 
>> “somewhere far”, but there is a lot of distortion noise. Any help
>> would be greatly appreciated!!!
>> 
>> 
>> 
>> Thank you,
>> 
>> 
>> 
>> Evgueni Tsygankov
>> 
>> www.sqlanswers.com <http://www.sqlanswers.com/>
>> 
>> 
>> 
>> 
>> ------------------------------------------------------------------------
>> 
>> 
>> _______________________________________________ Speex-dev mailing
>> list Speex-dev at xiph.org 
>> http://lists.xiph.org/mailman/listinfo/speex-dev


More information about the Speex-dev mailing list