Convert -1/+1 stream before block transmit

Status
Not open for further replies.

eurocrack

New member
Hello,

We are trying to create a new AudioStream class and it's not clear to us how we should convert/normalize a -1/+1 stream in the format used in the teensy audio lib.

What we tried:

for (int i = 0; i < AUDIO_BLOCK_SAMPLES; ++i) {
const float out = osc.next_sample() * 0.1f;
block->data = out * 2000.0 + 2050.0;
}
transmit(block);
release(block);

osc being a simple sine osc class for testing purposes.

Any guidance ? :)
 
First off, if you need floating point support, have you looked at the floating point expansions to the Teensy Audio Library made by Chip Audette?
https://github.com/chipaudette/OpenAudio_ArduinoLibrary

Chip upgrade most of the library, most importantly the AudioStream.

That said, I'm a little confused by your example. The audio samples in the normal library I believe are signed 16-bit numbers, so they range from -32768 to +32767.If you're trying to normalize to a -1.0 to +1.0 floating point format (and you don't want to use Chip's library) you would do

Code:
//Convert to floating point
float floatBlock[AUDIO_BLOCK_SAMPLES];
for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
    floatBlock[i] = (float)block->data[i] / 32768.0f
}

// floatBlock now contains the samples in -1.0 to +1.0 format. Do processing.....now convert back
for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
    block->data[i] = (int16_t)(floatBlock[i] * 32768.0f)
}

You could do proper rounding if you wish on the conversion back to int16_t but you probably get the idea.
 
Thank you for pointing us to the chipaudette lib, we didn't know it.

It turns out we were trying to perform float computation on a teensy 3.2, hence the problem... we are now thinking about fixed point or either upgrade the teensy to 3.6...

Thank you for your help :)
 
.. we are now thinking about fixed point or either upgrade the teensy to 3.6...

I think that's a smart plan, in that order.

If you end up using the floating point audio library please let Chip know. It's positive community feedback that encourages talented people like him to keep contributing.
 
Status
Not open for further replies.
Back
Top