2 #include <vorbis/vorbisfile.h>
10 #include <QApplication>
26 int written = fwrite(page->header, 1, page->header_len, fp);
27 written += fwrite(page->body, 1, page->body_len, fp);
34 Encoder(outfile, qualitylevel, metadata)
36 vorbis_comment_init(&
m_vc);
38 vorbis_info_init(&
m_vi);
40 ogg_packet_clear(&
m_op);
43 if (qualitylevel == 0)
45 if (qualitylevel == 1)
48 int ret = vorbis_encode_setup_vbr(&
m_vi, 2, 44100, quality);
51 LOG(VB_GENERAL, LOG_ERR, QString(
"Error initializing VORBIS encoder."
52 " Got return code: %1").arg(ret));
53 vorbis_info_clear(&
m_vi);
57 vorbis_encode_ctl(&
m_vi, OV_ECTL_RATEMANAGE_SET,
nullptr);
58 vorbis_encode_setup_init(&
m_vi);
64 ogg_packet header_main;
65 ogg_packet header_comments;
66 ogg_packet header_codebooks;
68 vorbis_analysis_headerout(&
m_vd, &
m_vc, &header_main, &header_comments,
71 ogg_stream_packetin(&
m_os, &header_main);
72 ogg_stream_packetin(&
m_os, &header_comments);
73 ogg_stream_packetin(&
m_os, &header_codebooks);
76 while ((result = ogg_stream_flush(&
m_os, &
m_og)))
78 if (!result || !
m_out)
81 if (ret2 !=
m_og.header_len +
m_og.body_len)
83 LOG(VB_GENERAL, LOG_ERR,
84 "Failed to write header to output stream.");
92 ogg_stream_clear(&
m_os);
93 vorbis_block_clear(&
m_vb);
94 vorbis_dsp_clear(&
m_vd);
95 vorbis_comment_clear(&
m_vc);
96 vorbis_info_clear(&
m_vi);
105 long realsamples = 0;
106 auto *chars = (
signed char *)
bytes;
108 realsamples = length / 4;
113 float** buffer = vorbis_analysis_buffer(&
m_vd, realsamples);
115 for (
long i = 0; i < realsamples; i++)
117 buffer[0][i] = ((chars[(i * 4) + 1] << 8) |
118 (chars[i * 4] & 0xff)) / 32768.0F;
119 buffer[1][i] = ((chars[(i * 4) + 3] << 8) |
120 (chars[(i * 4) + 2] & 0xff)) / 32768.0F;
123 vorbis_analysis_wrote(&
m_vd, realsamples);
125 while (vorbis_analysis_blockout(&
m_vd, &
m_vb) == 1)
127 vorbis_analysis(&
m_vb,
nullptr);
128 vorbis_bitrate_addblock(&
m_vb);
130 while (vorbis_bitrate_flushpacket(&
m_vd, &
m_op))
138 int result = ogg_stream_pageout(&
m_os, &
m_og);
143 if (ret !=
m_og.header_len +
m_og.body_len)
145 LOG(VB_GENERAL, LOG_ERR,
146 QString(
"Failed to write ogg data. Aborting."));
151 if (ogg_page_eos(&
m_og))