1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
/**********************************************************************
Audacity: A Digital Audio Editor
ExportOGG.cpp
Joshua Haberman
This program is distributed under the GNU General Public License, version 2.
A copy of this license is included with this source.
**********************************************************************/
#include "Export.h"
#include <wx/log.h>
#include <wx/stream.h>
#include <vorbis/vorbisenc.h>
#include "wxFileNameWrapper.h"
#include "ExportPluginHelpers.h"
#include "ExportPluginRegistry.h"
#include "FileIO.h"
#include "Mix.h"
#include "Tags.h"
#include "Track.h"
#include "PlainExportOptionsEditor.h"
namespace
{
enum : int {
OptionIDOGGQuality = 0
};
const ExportOption OGGQualityOption {
OptionIDOGGQuality, XO("Quality"),
5,
ExportOption::TypeRange,
{ 0, 10 }
};
class ExportOptionOGGEditor final : public ExportOptionsEditor
{
int mQualityUnscaled;
public:
ExportOptionOGGEditor()
{
mQualityUnscaled = *std::get_if<int>(&OGGQualityOption.defaultValue);
}
int GetOptionsCount() const override
{
return 1;
}
bool GetOption(int, ExportOption& option) const override
{
option = OGGQualityOption;
return true;
}
bool GetValue(ExportOptionID, ExportValue& value) const override
{
value = mQualityUnscaled;
return true;
}
bool SetValue(ExportOptionID, const ExportValue& value) override
{
if(auto num = std::get_if<int>(&value))
{
mQualityUnscaled = *num;
return true;
}
return false;
}
SampleRateList GetSampleRateList() const override
{
return {};
}
void Load(const audacity::BasicSettings& config) override
{
mQualityUnscaled = config.Read(wxT("/FileFormats/OggExportQuality"),50)/10;
}
void Store(audacity::BasicSettings& config) const override
{
config.Write(wxT("/FileFormats/OggExportQuality"), mQualityUnscaled * 10);
}
};
}
#define SAMPLES_PER_RUN 8192u
class OGGExportProcessor final : public ExportProcessor
{
struct
{
TranslatableString status;
double t0;
double t1;
unsigned numChannels;
std::unique_ptr<Mixer> mixer;
std::unique_ptr<FileIO> outFile;
wxFileNameWrapper fName;
// All the Ogg and Vorbis encoding data
ogg_stream_state stream;
ogg_page page;
ogg_packet packet;
vorbis_info info;
vorbis_comment comment;
vorbis_dsp_state dsp;
vorbis_block block;
bool stream_ok{false};
bool analysis_state_ok{false};
} context;
public:
~OGGExportProcessor() override;
bool Initialize(AudacityProject& project,
const Parameters& parameters,
const wxFileNameWrapper& filename,
double t0, double t1, bool selectedOnly,
double sampleRate, unsigned channels,
MixerOptions::Downmix* mixerSpec,
const Tags* tags) override;
ExportResult Process(ExportProcessorDelegate& delegate) override;
private:
static void FillComment(AudacityProject *project, vorbis_comment *comment, const Tags *metadata);
};
class ExportOGG final : public ExportPlugin
{
public:
ExportOGG();
int GetFormatCount() const override;
FormatInfo GetFormatInfo(int) const override;
std::unique_ptr<ExportOptionsEditor>
CreateOptionsEditor(int, ExportOptionsEditor::Listener*) const override;
std::unique_ptr<ExportProcessor> CreateProcessor(int format) const override;
};
ExportOGG::ExportOGG() = default;
int ExportOGG::GetFormatCount() const
{
return 1;
}
FormatInfo ExportOGG::GetFormatInfo(int) const
{
return {
wxT("OGG"), XO("Ogg Vorbis Files"), { wxT("ogg") }, 255, true
};
}
OGGExportProcessor::~OGGExportProcessor()
{
if(context.stream_ok)
ogg_stream_clear(&context.stream);
if(context.analysis_state_ok)
{
vorbis_comment_clear(&context.comment);
vorbis_block_clear(&context.block);
vorbis_dsp_clear(&context.dsp);
}
vorbis_info_clear(&context.info);
}
bool OGGExportProcessor::Initialize(AudacityProject& project,
const Parameters& parameters,
const wxFileNameWrapper& fName,
double t0, double t1, bool selectionOnly,
double sampleRate, unsigned numChannels,
MixerOptions::Downmix* mixerSpec,
const Tags* metadata)
{
context.t0 = t0;
context.t1 = t1;
context.numChannels = numChannels;
double quality = ExportPluginHelpers::GetParameterValue(parameters, 0, 5) / 10.0;
wxLogNull logNo; // temporarily disable wxWidgets error messages
// Many of the library functions called below return 0 for success and
// various nonzero codes for failure.
// Encoding setup
vorbis_info_init(&context.info);
if (vorbis_encode_init_vbr(&context.info, numChannels, (int)(sampleRate + 0.5), quality)) {
// TODO: more precise message
throw ExportException(_("Unable to export - rate or quality problem"));
}
context.outFile = std::make_unique<FileIO>(fName, FileIO::Output);
if (!context.outFile->IsOpened()) {
throw ExportException(_("Unable to open target file for writing"));
}
context.analysis_state_ok = vorbis_analysis_init(&context.dsp, &context.info) == 0 &&
vorbis_block_init(&context.dsp, &context.block) == 0;
// Set up analysis state and auxiliary encoding storage
if (!context.analysis_state_ok) {
throw ExportException(_("Unable to export - problem initialising"));
}
// Retrieve tags
FillComment(&project, &context.comment, metadata);
// Set up packet->stream encoder. According to encoder example,
// a random serial number makes it more likely that you can make
// chained streams with concatenation.
srand(time(NULL));
context.stream_ok = ogg_stream_init(&context.stream, rand()) == 0;
if (!context.stream_ok) {
throw ExportException(_("Unable to export - problem creating stream"));
}
// First we need to write the required headers:
// 1. The Ogg bitstream header, which contains codec setup params
// 2. The Vorbis comment header
// 3. The bitstream codebook.
//
// After we create those our responsibility is complete, libvorbis will
// take care of any other ogg bitstream constraints (again, according
// to the example encoder source)
ogg_packet bitstream_header;
ogg_packet comment_header;
ogg_packet codebook_header;
if(vorbis_analysis_headerout(&context.dsp, &context.comment, &bitstream_header, &comment_header,
&codebook_header) ||
// Place these headers into the stream
ogg_stream_packetin(&context.stream, &bitstream_header) ||
ogg_stream_packetin(&context.stream, &comment_header) ||
ogg_stream_packetin(&context.stream, &codebook_header)) {
throw ExportException(_("Unable to export - problem with packets"));
}
// Flushing these headers now guarantees that audio data will
// start on a NEW page, which apparently makes streaming easier
while (ogg_stream_flush(&context.stream, &context.page)) {
if ( context.outFile->Write(context.page.header, context.page.header_len).GetLastError() ||
context.outFile->Write(context.page.body, context.page.body_len).GetLastError()) {
throw ExportException(_("Unable to export - problem with file"));
}
}
context.mixer = ExportPluginHelpers::CreateMixer(
project, selectionOnly, t0, t1, numChannels, SAMPLES_PER_RUN, false,
sampleRate, floatSample, mixerSpec);
context.status = selectionOnly
? XO("Exporting the selected audio as Ogg Vorbis")
: XO("Exporting the audio as Ogg Vorbis");
return true;
}
ExportResult OGGExportProcessor::Process(ExportProcessorDelegate& delegate)
{
delegate.SetStatusString(context.status);
auto exportResult = ExportResult::Success;
{
int err;
int eos = 0;
while (exportResult == ExportResult::Success && !eos) {
float **vorbis_buffer = vorbis_analysis_buffer(&context.dsp, SAMPLES_PER_RUN);
auto samplesThisRun = context.mixer->Process();
if (samplesThisRun == 0) {
// Tell the library that we wrote 0 bytes - signalling the end.
err = vorbis_analysis_wrote(&context.dsp, 0);
}
else {
for (size_t i = 0; i < context.numChannels; i++) {
float *temp = (float *)context.mixer->GetBuffer(i);
memcpy(vorbis_buffer[i], temp, sizeof(float)*SAMPLES_PER_RUN);
}
// tell the encoder how many samples we have
err = vorbis_analysis_wrote(&context.dsp, samplesThisRun);
}
// I don't understand what this call does, so here is the comment
// from the example, verbatim:
//
// vorbis does some data preanalysis, then divvies up blocks
// for more involved (potentially parallel) processing. Get
// a single block for encoding now
while (!err && vorbis_analysis_blockout(&context.dsp, &context.block) == 1) {
// analysis, assume we want to use bitrate management
err = vorbis_analysis(&context.block, NULL);
if (!err)
err = vorbis_bitrate_addblock(&context.block);
while (!err && vorbis_bitrate_flushpacket(&context.dsp, &context.packet)) {
// add the packet to the bitstream
err = ogg_stream_packetin(&context.stream, &context.packet);
// From vorbis-tools-1.0/oggenc/encode.c:
// If we've gone over a page boundary, we can do actual output,
// so do so (for however many pages are available).
while (!err && !eos) {
int result = ogg_stream_pageout(&context.stream, &context.page);
if (!result) {
break;
}
if ( context.outFile->Write(context.page.header, context.page.header_len).GetLastError() ||
context.outFile->Write(context.page.body, context.page.body_len).GetLastError()) {
// TODO: more precise message
throw ExportDiskFullError(context.fName);
}
if (ogg_page_eos(&context.page)) {
eos = 1;
}
}
}
}
if (err) {
// TODO: more precise message
throw ExportErrorException("OGG:355");
}
exportResult = ExportPluginHelpers::UpdateProgress(
delegate, *context.mixer, context.t0, context.t1);
}
}
if ( !context.outFile->Close() ) {
// TODO: more precise message
throw ExportErrorException("OGG:366");
}
return exportResult;
}
std::unique_ptr<ExportOptionsEditor>
ExportOGG::CreateOptionsEditor(int, ExportOptionsEditor::Listener*) const
{
return std::make_unique<ExportOptionOGGEditor>();
}
std::unique_ptr<ExportProcessor> ExportOGG::CreateProcessor(int format) const
{
return std::make_unique<OGGExportProcessor>();
}
void OGGExportProcessor::FillComment(AudacityProject *project, vorbis_comment *comment, const Tags *metadata)
{
// Retrieve tags from project if not over-ridden
if (metadata == NULL)
metadata = &Tags::Get( *project );
vorbis_comment_init(comment);
wxString n;
for (const auto &pair : metadata->GetRange()) {
n = pair.first;
const auto &v = pair.second;
if (n == TAG_YEAR) {
n = wxT("DATE");
}
vorbis_comment_add_tag(comment,
(char *) (const char *) n.mb_str(wxConvUTF8),
(char *) (const char *) v.mb_str(wxConvUTF8));
}
}
static ExportPluginRegistry::RegisteredPlugin sRegisteredPlugin{ "OGG",
[]{ return std::make_unique< ExportOGG >(); }
};
|