Sample sizes in Qt audio example



  • Hello,
    I've started to recently dive into Qt and I've been playing with the Qt audio example. However I ran into a problem. When I change the sample size from the default 8 to 16 then the visual representation starts to go weird. It constantly shows as if there was sound (even if the microphone is turned off or when it's completely quiet) but you can tell when there are loud noises for example tapping on the microphone. I've tried changing the visual representation (x and y on the chart) but that doesn't seem to fix it. The microphone is recording in dual channel 16 bit 44100 Hz so compatibility with 16 bit shouldn't be an issue either. Any suggestions?
    Thank you for your time.



  • remember that sound samples can be signed or unsigned. What happens when you resize a byte to a short with respect to the two's complement value?

    See enum QAudioFormat::SampleType



  • I don't think this is the problem here, the same thing occurs with either signed or unsigned int. Or were you suggesting a different change?



  • I'm not suggesting a change. I'm trying to give you some hints as to where your problem lies. Do you understand how PCM is used to generate a sound wave form? or what happens to a digital sound sample when the availabe bitrange is changed or it's format is modified?

    As a simple example question:

    you have an unsigned 8 bit sound sample and you cast it to an unsigned short value of 16 bits? What happens to the volume of that sample?



  • Thank you for your help, I've probably identified the problem - one of the input parameters was a char (8 bit). I've tried to fix it using the following code to reformat it so that it can be used properly for 16 bit but I keep getting the same problem

    /* Function with char as one of the parameters - qint64 XYSeriesIODevice::writeData(const char *data, qint64 maxSize) - same as in the Qt audio example**/
    
        int bitsPerSample = 16;
        int bytesPerSample = (bitsPerSample / BITS_IN_BYTE);
        int signalLength = (strlen(data) ) / numChannels / bytesPerSample;
        short* signal = (short*)malloc(sizeof(short) * signalLength);
    
        for(int i = 0; i < signalLength; i++){
            int index =  i * numChannels * bytesPerSample;
    
            memcpy(&signal[i], &data[index], sizeof(short));
        }
    

    Any suggestions?