Waitrud Weber’s blog

things and reminders for memories

music: sounds: Reducing buffer size.

The program sounds well.
We need 128 buffer size in this case.
I change the base source code for that.

.\main.cpp  Thu Jun 24 17:42:20 2021
...
138 :int main(int argc, char** argv) {
139 :
140 :	sound_000a = new SoundEffect( sbuffer, 128, 2200 );
141 :	sound_000a->Play();
142 :
143 :	return 0;
144 :}
...
.\sounds-011.h  Thu Jun 24 17:41:54 2021
...
 22 :    SoundEffect(const int noteInfo[], int fix_Arraysize, const int VALHZ)
 23 :    {
 24 :        // Initialize the sound format we will request from sound card
 25 :        m_waveFormat.wFormatTag = WAVE_FORMAT_PCM;     // Uncompressed sound format
 26 :        m_waveFormat.nChannels = 1;                    // 1 = Mono, 2 = Stereo
 27 :        m_waveFormat.wBitsPerSample = 8;               // Bits per sample per channel
 28 :        m_waveFormat.nSamplesPerSec = VALHZ;           // Sample Per Second
 29 ://        m_waveFormat.nSamplesPerSec = 11025;           // Sample Per Second
 30 :        m_waveFormat.nBlockAlign = m_waveFormat.nChannels * m_waveFormat.wBitsPerSample / 8;
 31 :        m_waveFormat.nAvgBytesPerSec = m_waveFormat.nSamplesPerSec * m_waveFormat.nBlockAlign;
 32 :        m_waveFormat.cbSize = 0;
 33 :
 34 :
 35 :		const int ramda = 8;
 36 :		int count_v = 0;
 37 :
 38 :        int dataLength = 0, moment = (m_waveFormat.nSamplesPerSec / 75);
 39 :        double period = 2.0 * PI / (double) m_waveFormat.nSamplesPerSec;
 40 :
 41 :        // Calculate how long we need the sound buffer to be
 42 :        for (int i = 1; i < fix_Arraysize; i += 2)
 43 :            dataLength += (noteInfo[i] != 0) ? noteInfo[i] * moment : moment;
 44 :
 45 :        // Allocate the array
 46 :        m_data = new char[m_bufferSize = dataLength];
 47 :
 48 :        int placeInData = 0;
 49 :
 50 :        // Make the sound buffer
 51 :        for (int i = 0; i < fix_Arraysize; i += 2)
 52 :        {
 53 :            int relativePlaceInData = placeInData;
 54 :
 55 :            while ((relativePlaceInData - placeInData) < ((noteInfo[i + 1] != 0) ? noteInfo[i + 1] * moment : moment))
 56 :            {
 57 :                // Generate the sound wave (as a sinusoid)
 58 :                // - x will have a range of -1 to +1
 59 :                double x = sin((relativePlaceInData - placeInData) * 55 * pow(HALF_NOTE, noteInfo[i]) * period);
 60 :
 61 :				count_v = relativePlaceInData % ramda;
 62 :
 63 :                // Scale x to a range of 0-255 (signed char) for 8 bit sound reproduction
 64 :                m_data[relativePlaceInData] = (char) (127 * x + 128);
 65 :                m_data[relativePlaceInData] /= ramda;
 66 :                m_data[relativePlaceInData] *= count_v;
 67 :
 68 :                relativePlaceInData++;
 69 :            }
 70 :
 71 :            placeInData = relativePlaceInData;
 72 :        }
 73 :    }
...