0

I cannot convert between 16 bit Scaled PCM Data and Floating Point PCM Data in C++. I think I must be close because the output audio somewhat resembles what I expect, but it is distorted.

The reason I am doing this is because I am running ScummVM games in the browser. The ScummVM code runs on the server and my custom code posts audio and images up to a website. I am using the 'Web-Audio Api' to play sound in JavaScript on the front end.

I am trying to provide raw PCM data segmented by channel to the JavaScript. My logic here is that there will be less latency if no decoding is required.

I know that the audio data must be good, because I have successfully converted this into a wave file format and played it.

I am trying to get a multidimensional array of floats. The first dimension represents the channel and the second is the sample data for that channel.

I ask that we not discuss the usefulness of the project. This is a learning project for me, not an attempt to build a widely used product.

Here is my algorithm, with comments describing my reasoning.

C++ Side

typedef unsigned char byte;
double** OSystem_Cli::mixCallback(uint len)
{
    const int NO_CHANNELS = 2;
    double** result = new double*[NO_CHANNELS];
    int sampleSize = len;
    byte* samples = new byte[len];

    _mixerImpl->mixCallback(samples, len);  //Get the 16-bit PCM audio from Scumm VM. eg. [91,11,91,11 . . .] and stores it in samples

    for (int channel = 0; channel < NO_CHANNELS; channel++)
    {
        for (int byteNo = channel * 2, channelByteNo = 0; byteNo < sampleSize - 1; byteNo = byteNo + NO_CHANNELS * 2, channelByteNo++)
        {
            if (channelByteNo == 0)
            {
                result[channel] = new double[sampleSize / NO_CHANNELS / 2];
            }
            unsigned short unsignedCombination = (static_cast<unsigned short>(samples[byteNo]) << 8) + samples[byteNo + 1]; //Join two bytes together to get 1 sixteen bit number. 
            short signedCombination;

            memcpy(&signedCombination, &unsignedCombination, sizeof(signedCombination));

            double signedFloat = static_cast<double>(signedCombination);
            signedFloat = signedFloat / (float)32768;  //Divide it to get the floating point representation, as https://stackoverflow.com/questions/15087668/how-to-convert-pcm-samples-in-byte-array-as-floating-point-numbers-in-the-range states.

            if (signedFloat > 1)
            {
                signedFloat = 1;
            }
            else if (signedFloat < -1)
            {
                signedFloat = -1;
            }
            result[channel][channelByteNo] = signedFloat;

        }
    }
    delete[] samples;

    return result;
}

JavaScript Side (In type script):

   pushOntoAudioQueue(pcmBytesByChannel: number[][]) {
        const streamer: WebAudioStreamer = this;
        const samplePartsPerChannel = pcmBytesByChannel[0].length;
        const noChannels = pcmBytesByChannel.length;

        if (this.audioStack.length > MaxQueueLength) {
            this.audioStack = [];
        }

        const buffer = this.context.createBuffer(noChannels, samplePartsPerChannel, 16000); //Length is total number of bytes for all channels

        for (let channel = 0; channel < noChannels; channel++) {
            let float32ChannelData = new Float32Array(pcmBytesByChannel[channel].length); 
            float32ChannelData.set(pcmBytesByChannel[channel]);
            buffer.copyToChannel(float32ChannelData, channel);
        }

       streamer.audioQueue.push(buffer);
    }
  • `cli::array^>^ result` -- This is not C++. – PaulMcKenzie Apr 24 '20 at 13:19
  • 1
    That's C++ CLI notation for a managed array. C++ CLI is a bridge between managed and unmanaged code in the Microsoft world. As I said in my comment treat in as double[][]. The C++ CLI part has very little bearing on the question. The number crunching still works the same. – user2732707 Apr 24 '20 at 13:44
  • There is a tag for this. Just tagging C++ is misleading. – PaulMcKenzie Apr 24 '20 at 13:45

1 Answers1

0

I got this answer to this from another forum. My endianness was wrong I was assuming big endianess when the PCM data was little endianess.

Please see: http://cplusplus.com/forum/general/269888/