Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Convert -1/+1 stream before block transmit

  1. #1
    Junior Member
    Join Date
    Jul 2017
    Posts
    4

    Convert -1/+1 stream before block transmit

    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[i] = out * 2000.0 + 2050.0;
    }
    transmit(block);
    release(block);

    osc being a simple sine osc class for testing purposes.

    Any guidance ? :-)

  2. #2
    Senior Member Blackaddr's Avatar
    Join Date
    Mar 2017
    Location
    Canada
    Posts
    194
    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.

  3. #3
    Junior Member
    Join Date
    Jul 2017
    Posts
    4
    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 :-)

  4. #4
    Senior Member Blackaddr's Avatar
    Join Date
    Mar 2017
    Location
    Canada
    Posts
    194
    Quote Originally Posted by eurocrack View Post
    .. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •