66 * by Lauren Kelly (thejsa) with help from mtheall.
77 * See the opus-decoding example for more details
88 *
9- * Last update: 2024-03-31
9+ * Last update: 2026-04-22
1010 */
1111
1212#define ARRAY_SIZE (x ) (sizeof(x) / sizeof((x)[0]))
@@ -129,7 +129,7 @@ bool audioInit(OggVorbis_File *vorbisFile_) {
129129
130130 for (size_t i = 0 ; i < ARRAY_SIZE (s_waveBufs ); ++ i ) {
131131 s_waveBufs [i ].data_vaddr = buffer ;
132- s_waveBufs [i ].nsamples = WAVEBUF_SIZE / sizeof ( buffer [ 0 ]);
132+ s_waveBufs [i ].nsamples = SAMPLES_PER_BUF ; // stereo = two s16 played in parallel = still one sample for the dsp
133133 s_waveBufs [i ].status = NDSP_WBUF_DONE ;
134134
135135 buffer += WAVEBUF_SIZE / sizeof (buffer [0 ]);
@@ -154,11 +154,13 @@ bool fillBuffer(OggVorbis_File *vorbisFile_, ndspWaveBuf *waveBuf_) {
154154 osTickCounterStart (& timer );
155155 #endif // DEBUG
156156
157+ const size_t CHANNELS_PER_SAMPLE = ndspChnGetFormat (0 ) == NDSP_FORMAT_STEREO_PCM16 ? 2 : 1 ;
158+
157159 // Decode (2-byte) samples until our waveBuf is full
158160 int totalBytes = 0 ;
159- while (totalBytes < waveBuf_ -> nsamples * sizeof (s16 )) {
161+ while (totalBytes < waveBuf_ -> nsamples * sizeof (s16 ) * CHANNELS_PER_SAMPLE ) {
160162 int16_t * buffer = waveBuf_ -> data_pcm16 + (totalBytes / sizeof (s16 ));
161- const size_t bufferSize = (waveBuf_ -> nsamples * sizeof (s16 ) - totalBytes );
163+ const size_t bufferSize = (waveBuf_ -> nsamples * sizeof (s16 ) * CHANNELS_PER_SAMPLE - totalBytes );
162164
163165 // Decode bufferSize bytes from vorbisFile_ into buffer,
164166 // storing the number of bytes that were read (or error)
@@ -185,7 +187,7 @@ bool fillBuffer(OggVorbis_File *vorbisFile_, ndspWaveBuf *waveBuf_) {
185187 // = for most cases
186188 // < for the last possible chunk of the file, which may have less samples before EOF
187189 // after which we don't care to recover the length
188- waveBuf_ -> nsamples = totalBytes / sizeof (s16 );
190+ waveBuf_ -> nsamples = totalBytes / sizeof (s16 ) / CHANNELS_PER_SAMPLE ;
189191 ndspChnWaveBufAdd (0 , waveBuf_ );
190192 DSP_FlushDataCache (waveBuf_ -> data_pcm16 , totalBytes );
191193
0 commit comments