Totally at a loss, getting nothing but static

Dec 3, 2012 at 11:03 PM
Edited Dec 3, 2012 at 11:13 PM

Hi all,

Just to preface, I've been working on getting this to work for a few days now. I've read most every relevant post on the discussion forums, looked at the source and its assorted examples, and worked over the documentation. For the life of me I can't figure out why I can't get NSpeex to work.

My input audio is 32-bit float, stereo.

I've tried both encoding in chunks at the FrameSize, assuming that the desired input is 16-bit shorts with the 32-bit floats downsampled; and I've tried encoding the entire package in a go, with the floats taking up 2 shorts and no loss of data in the conversion. Both just give me noise.

I've tried with artificial input (a sin wave), and can't see much of a pattern in what's coming out.

Dec 3, 2012 at 11:16 PM
Edited Dec 4, 2012 at 12:10 AM

Turns out I was converting the PCM incorrectly. 

Dec 4, 2012 at 4:08 PM
Your conversion from float to short does not seem to be correct, conversion should NOT be done based on bytes!

Sampling in float means that the value of one sample is represented in a float value. Since any audio codec is expecting real samples and not split into two samples it would not work as you have done.

Try to sample in short.

On Tue, Dec 4, 2012 at 12:16 AM, dleslie <> wrote:

From: dleslie

Here's the implementation I have thus far:

public static byte[] EncodeSpeex(short[] in_buffer) { int input_length = in_buffer.Length; input_length -= input_length % encoder.FrameSize; byte[] out_buffer = new byte[input_length * 2]; int encoded_bytes = encoder.Encode(in_buffer, 0, input_length, out_buffer, 0, input_length * 2); byte[] small_buffer = new byte[encoded_bytes]; System.Array.Copy(out_buffer, 0, small_buffer, 0, encoded_bytes); return small_buffer; } public static short[] DecodeSpeex(byte[] encoded_data) { short[] decoded_chunk = new short[encoded_data.Length * 8]; int decoded_bytes = decoder.Decode(encoded_data, 0, encoded_data.Length, decoded_chunk, 0, false); short[] small_chunk = new short[decoded_bytes / 2]; System.Buffer.BlockCopy(decoded_chunk, 0, small_chunk, 0, decoded_bytes / 2); return small_chunk; } public static short[] ExtractMonoRecording(float[] audio_data) { short[] short_data = new short[audio_data.Length]; for(int idx = 0; idx < short_data.Length; idx += 2) { // Skip stereo float sample = audio_data[idx]; short_data[idx] = System.BitConverter.ToInt16(System.BitConverter.GetBytes(sample), 0); short_data[idx + 1] = System.BitConverter.ToInt16(System.BitConverter.GetBytes(sample), 2); } return short_data; }
public static float[] InjectMonoRecording(short[] short_data) { float[] audio_data = new float[short_data.Length]; for(int idx = 0; idx < short_data.Length; idx += 2) { System.Buffer.BlockCopy(short_data, idx * 2, audio_data, idx * 4, 4); System.Buffer.BlockCopy(short_data, idx * 2, audio_data, (idx + 1) * 4, 4); } return audio_data; }

Read the full discussion online.

To add a post to this discussion, reply to this email (

To start a new discussion for this project, email

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at

Dec 4, 2012 at 4:11 PM

Sampling in short wasn't an option. But, I found a conversion that worked (a facepalm moment, clearly I just needed to move from the range -1, 1 to one that would fit in a signed short). 

Jan 5, 2013 at 5:07 AM

Would it be too much to ask to post your working solution? I must be having a similar issue... I'm getting the right data size back, but I'm only getting loud static :(

Jan 7, 2013 at 3:05 PM

Nevermind! I misread your previous comment and I did the same thing! And had the same facepalm! Of course, a basic conversion from float to short would get you -1, 1, 0 values, so an easy value-mapping algorithm is all you need.