Audiobuffer = float?

This is rather a problem related to my programming skills than Juce, I hope you don’t mind me asking this here anyways.

I never used C++ until I found this wonderful Framework, thanks for all the effort you put in developing this, my first steps in C++ couldn’t have been made easier.

What I want to do is very simple, have a VST that loads WAVs and plays them back when it is triggered by a MIDI-note.

I got the file to load and wrote the data in a Byte array like I am used to from OpenAL programming while Juce wants to have a float input for the sample buffer. I am a bit lost here.
It’s a 16bit WAVE file and I have no idea how to get the float representation. If I only add the first byte to the buffer I have 8bit audio, how do I add the second byte?

(-127 + Firstbyte) * 255 + (Secondbyte) is what I used to do in other programs (pascal), but this does (of course) only work with int16 values.

I have set the hosts bit rate to 16bit btw, and the sample rate is also the same, to make it as easy as possible.

1 Like

You should look into: http://en.wikipedia.org/wiki/IEEE_754-1985 floating points are represented ina different way than normal numbers (sign, exponent, fraction).

also i don’t think you should be doing this manualy, there are some btter ways to convert ints to floats, and juce has them, look here
http://www.rawmaterialsoftware.com/juce/api/juce__DataConversions_8h.html#39c362057ff4dab1b1601aefd62848a0

How did you even get as far as thinking about converting values manually?? Did you look at the juce demo, where it’s got a synth that loads and triggers audio files, all without doing a single sample conversion…

i wanted the gentle approach :slight_smile:

Thanks for the answers so far.
Yes I’ve taken a look at the demo before. As I plan on using some kind of library and operate further on the PCM data I thought that this is the best approach. I have to get my hands on each sample anyway, so why not prepare this at loading time?

Thanks for pointing me in the right direction atom.
I did use the AudioDataConverters class to get an array of floats, which works more or less. But I get absolutely no audio output.
The floats range is between -1 and +1, and the int16 between -5000 and +5000.

like jules wrote, don’t read the file using your own I/O use the JUCE AudioFormat classes for reading the file. If it’s a WAV file it JUCE will do all the conversion work for you.

Well okay. Did it and it works, the float values do not seem to differ from what I have done so I have no idea why it works now, but I don’t really care as long as it does.

Thanks for helping.

Sorry, I didn’t intend my answer to sound so harsh!

It’s always smarter to let someone else worry about decoding the files, and just do your processing on the floating point data that comes out.

On a related note, is there a way to convert the audio data from float back to int16?

For recording an audio stream through micphone, usually following interface function is implemented:
void AudioIODeviceCallback::audioDeviceIOCallback ( const float ** inputChannelData,
int numInputChannels,
float ** outputChannelData,
int numOutputChannels,
int numSamples
)

The input channel data are floating numbers. Can the floating number data be converted to int16 within JUCE? Or what is the JUCE way to do the conversion?

[quote=“AlexY”]On a related note, is there a way to convert the audio data from float back to int16?

For recording an audio stream through micphone, usually following interface function is implemented:
void AudioIODeviceCallback::audioDeviceIOCallback ( const float ** inputChannelData,
int numInputChannels,
float ** outputChannelData,
int numOutputChannels,
int numSamples
)

The input channel data are floating numbers. Can the floating number data be converted to int16 within JUCE? Or what is the JUCE way to do the conversion?[/quote]
Found a function: AudioDataConverters::convertFloatToInt16LE, is that the intended way in JUCE?

That or the more flexible AudioData template classes.