Distorted sound when feeding data to jack - .wav binary data into digital signals?

StackOverflow https://stackoverflow.com/questions/14035231

  •  12-12-2021
  •  | 
  •  

After a bit of troubleshooting I realized ( at least I'm pretty sure ) I have been feeding the wrong kind of values to sound ports via the JackAudio library.

This is the callback function that is currently working but producing incredibly distorted sounds.

I suspect I have to turn my binary ( then later converted to decimal ) data into a float signal between -1 to 1.

How can I do the latter?

Right now I'm feeding it a 16-bit wave music file. Each sample is of size short.

static int Process( jack_nframes_t nframes, void * arg )
    {

        SamplerClass * SamplerPtr = ( SamplerClass * ) arg;

        jack_default_audio_sample_t * LeftChannel, * RightChannel;

        LeftChannel = ( jack_default_audio_sample_t * ) jack_port_get_buffer( LeftChannelOutputPort, nframes );

        RightChannel = ( jack_default_audio_sample_t * ) jack_port_get_buffer( RightChannelOutputPort, nframes );

        for( unsigned int i = 0; i<nframes; i++)
        {
            LeftChannel[i] = SamplerPtr->SoundFile->getSoundDataRef().at( SamplerPtr->SamplePosition ) ;
            RightChannel[i] = SamplerPtr->SoundFile->getSoundDataRef().at( SamplerPtr->SamplePosition + 1;

            SamplerPtr->SamplePosition = SamplerPtr->SamplePosition + 2;
        }

        return 0;
    }

getSoundDataRef() returns a vector<short> and I get the sample position via vector::at.

I'm keeping track of the sample position from a public int variable via SamplePtr->SamplePosition;

Here's debug output of the data format that's inside the vector for a 16-bit 41000Hz stereo wave sample. So it seems the data assigned to the channels is correct.

[INFO] [ 18:48:50.492] 288756 vec index RCh >1844
[INFO] [ 18:48:50.492] 288757 vec index LCh >1401
[INFO] [ 18:48:50.492] 288758 vec index RCh >-1251
有帮助吗?

解决方案

As in the comments discussion, the solution is to pass the CORRECT data format to teh Jackaudio library - in this case, the format is floating point in the range -1.0 - 1.0, where the original data is short int, so converting each sample with the formula x = sample / 32767.0; will give the desired result.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top