Skip to main content


Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Need help with new input component that has distortion (Read 963 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Need help with new input component that has distortion

I have created a new component from scratch for Professional Music Driver files.

The alpha version works as in, it generates sound, however the sound is incredibly distorted. A reference console implementation using SDL plays the same file without a problem. So I think the problem must be in my foobar2000 wrapper code.

Before I release an alpha of the plugin and the code I'd like to get this distortion problem solved.

A long shot: is there a developer with more audio experience that can point me in the right direction simply by listening to the example file?

Some parameters:

- The driver outputs 16-bit signed integers in blocks of 512 stereo samples.
- Sample rate 44100 Hz
- 2 channels

This is the core of decode_run()
Code: [Select]
        const uint32_t SamplesToRender = _Decoder->GetSampleCount();
        const uint32_t ChannelCount = _Decoder->GetChannelCount();

        audioChunk.set_data_size((t_size) SamplesToRender * ChannelCount);

        audio_sample * Samples = audioChunk.get_data();

        size_t SamplesRendered =_Decoder->Render(Samples, SamplesToRender);

        if (SamplesRendered == 0)
            return false;


        _SamplesRendered += SamplesRendered;

And this converts the samples to an audio chunk:
Code: [Select]
size_t PMDDecoder::Render(audio_sample * samples, size_t sampleCount) const noexcept
    ::getpcmdata(_Samples, (int) SampleCount);

    audio_math::convert_from_int16(_Samples, SampleCount, samples, (audio_sample) 2.0);

    return sampleCount;

Sample (512K)

Re: Need help with new input component that has distortion

Reply #1
The scale provided in that call to convert_from_int16 seems a bit suspicious.
The implementation goes roughly like this:
Code: [Select]
// computed from argument, here 2.0
float scale = (float)(p_scale / (double)0x8000);

// scaling each int16 sample in input range [-0x8000, 0x7FFF] by 2.0/0x8000
// resulting in an output range of [-2.0, 2.0) (that is, [-0x8000*2.0/0x8000, 0x7FFF*2.0/0x8000])
*(p_output++) = (float_t)*(p_source++) * scale;
You probably want a scale of 1.0.
Stay sane, exile.

Re: Need help with new input component that has distortion

Reply #2
Thank you for the reply.

I experimented with the scale parameter but the only audible result was that the output was softer (between 0.5 and 1.0) or louder (between 1.0 and 2.0). In the frequency analyzer the power of each frequency reflects that. Below 0.5 no sound is produced.

However the distortion remained.

I'm pretty sure the output of the decoder is correct because I fed it into the reference implementation and it played without distortion. A capture of the reference implementation fed into the foobar2000 component resulted in distorted sound.

Any ideas which other audio parameters that may be the cause?

Re: Need help with new input component that has distortion

Reply #3
Fixed it. As I suspected the audio math was wrong. I switched to calling audio_chunk::.set_data_fixedpoint() and now it works.