@@ -54,6 +54,40 @@ bool MDAudioVorbis::updateSection()
5454 return true ;
5555}
5656
57+ void MDAudioVorbis::writeFlush ()
58+ {
59+ int eos = 0 ;
60+
61+ while (vorbis_analysis_blockout (&m_vd, &m_vb) == 1 )
62+ {
63+ /* analysis, assume we want to use bitrate management */
64+ vorbis_analysis (&m_vb, nullptr );
65+ vorbis_bitrate_addblock (&m_vb);
66+
67+ while (vorbis_bitrate_flushpacket (&m_vd, &m_op))
68+ {
69+ /* weld the packet into the bitstream */
70+ ogg_stream_packetin (&m_os, &m_op);
71+
72+ /* write out pages (if any) */
73+ while (!eos)
74+ {
75+ int result = ogg_stream_pageout (&m_os, &m_og);
76+ if (result == 0 )
77+ break ;
78+
79+ SDL_RWwrite (m_file, m_og.header , 1 , m_og.header_len );
80+ SDL_RWwrite (m_file, m_og.body , 1 , m_og.body_len );
81+
82+ /* this could be set above, but for illustrative purposes, I do
83+ it here (to show that vorbis does know where the stream ends) */
84+ if (ogg_page_eos (&m_og))
85+ eos = 1 ;
86+ }
87+ }
88+ }
89+ }
90+
5791MDAudioVorbis::MDAudioVorbis () : MDAudioFile()
5892{}
5993
@@ -289,6 +323,8 @@ bool MDAudioVorbis::close()
289323 if (m_file)
290324 {
291325 vorbis_analysis_wrote (&m_vd, 0 );
326+ writeFlush ();
327+
292328 ogg_stream_clear (&m_os);
293329 vorbis_block_clear (&m_vb);
294330 vorbis_dsp_clear (&m_vd);
@@ -321,10 +357,11 @@ size_t MDAudioVorbis::readChunk(uint8_t *out, size_t outSize, bool *spec_changed
321357#else
322358 amount = (int )ov_read (&m_vf, (char *)out, outSize, SDL_BYTEORDER == SDL_BIG_ENDIAN, 2 , 1 , &cur_section);
323359#endif
324- if (amount < 0 )
360+
361+ if (amount < 0 )
325362 {
326363 set_ov_error (" ov_read" , amount);
327- return 0 ;
364+ return ~( size_t ) 0 ;
328365 }
329366
330367 if (cur_section != m_section)
@@ -344,7 +381,7 @@ size_t MDAudioVorbis::readChunk(uint8_t *out, size_t outSize, bool *spec_changed
344381size_t MDAudioVorbis::writeChunk (uint8_t *in, size_t inSize)
345382{
346383 long i;
347- int eos = 0 ;
384+ size_t written = 0 ;
348385 int sample_size = m_spec.m_channels * sizeof (short );
349386 /* expose the buffer to submit data */
350387
@@ -359,36 +396,10 @@ size_t MDAudioVorbis::writeChunk(uint8_t *in, size_t inSize)
359396 buffer[c][i] = *(in_s++) / 32768 .f ;
360397 }
361398
399+ written += i * sample_size;
362400 vorbis_analysis_wrote (&m_vd, i);
363401
364- while (vorbis_analysis_blockout (&m_vd, &m_vb) == 1 )
365- {
366- /* analysis, assume we want to use bitrate management */
367- vorbis_analysis (&m_vb, nullptr );
368- vorbis_bitrate_addblock (&m_vb);
369-
370- while (vorbis_bitrate_flushpacket (&m_vd, &m_op))
371- {
372- /* weld the packet into the bitstream */
373- ogg_stream_packetin (&m_os, &m_op);
374-
375- /* write out pages (if any) */
376- while (!eos)
377- {
378- int result = ogg_stream_pageout (&m_os, &m_og);
379- if (result == 0 )
380- break ;
381-
382- SDL_RWwrite (m_file, m_og.header , 1 , m_og.header_len );
383- SDL_RWwrite (m_file, m_og.body , 1 , m_og.body_len );
384-
385- /* this could be set above, but for illustrative purposes, I do
386- it here (to show that vorbis does know where the stream ends) */
387- if (ogg_page_eos (&m_og))
388- eos = 1 ;
389- }
390- }
391- }
402+ writeFlush ();
392403
393- return 0 ;
404+ return written ;
394405}
0 commit comments