| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2018 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #ifdef __WIN32__ |
| 14 | # include <windows.h> |
| 15 | # include <mmreg.h> |
| 16 | #else |
| 17 | # define WAVE_FORMAT_PCM 0x0001 |
| 18 | # define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
| 19 | #endif |
| 20 | |
| 21 | #include "wave.h" |
| 22 | |
| 23 | using namespace smooth::IO; |
| 24 | |
| 25 | const String &BoCA::EncoderWave::GetComponentSpecs() |
| 26 | { |
| 27 | static String componentSpecs = " \ |
| 28 | \ |
| 29 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 30 | <component> \ |
| 31 | <name>Windows Wave File Output</name> \ |
| 32 | <version>1.0</version> \ |
| 33 | <id>wave-enc</id> \ |
| 34 | <type>encoder</type> \ |
| 35 | <format> \ |
| 36 | <name>Microsoft Wave Files</name> \ |
| 37 | <extension>wav</extension> \ |
| 38 | <tag id=\"riff-tag\" mode=\"other\">RIFF INFO Tag</tag> \ |
| 39 | <tag id=\"cart-tag\" mode=\"other\">RIFF Cart Tag</tag> \ |
| 40 | <tag id=\"id3v2-tag\" mode=\"other\">ID3v2</tag> \ |
| 41 | </format> \ |
| 42 | <input bits=\"8\" signed=\"false\"/> \ |
| 43 | <input bits=\"16-32\"/> \ |
| 44 | </component> \ |
| 45 | \ |
| 46 | "; |
| 47 | |
| 48 | return componentSpecs; |
| 49 | } |
| 50 | |
| 51 | BoCA::EncoderWave::EncoderWave() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | BoCA::EncoderWave::~EncoderWave() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | Bool BoCA::EncoderWave::Activate() |
| 60 | { |
| 61 | Buffer<unsigned char> buffer(44); |
| 62 | OutStream *out = new OutStream(STREAM_BUFFER, buffer, 44); |
| 63 | |
| 64 | const Format &format = track.GetFormat(); |
| 65 | |
| 66 | out->OutputString("RIFF"); |
| 67 | out->OutputNumber(track.length * format.channels * (format.bits / 8) + 36, 4); |
| 68 | out->OutputString("WAVE"); |
| 69 | |
| 70 | out->OutputString("fmt "); |
| 71 | out->OutputNumber(16, 4); |
| 72 | out->OutputNumber(WAVE_FORMAT_PCM, 2); |
| 73 | out->OutputNumber(format.channels, 2); |
| 74 | out->OutputNumber(format.rate, 4); |
| 75 | out->OutputNumber(format.rate * format.channels * (format.bits / 8), 4); |
| 76 | out->OutputNumber(format.channels * (format.bits / 8), 2); |
| 77 | out->OutputNumber(format.bits, 2); |
| 78 | |
| 79 | out->OutputString("data"); |
| 80 | out->OutputNumber(track.length * format.channels * (format.bits / 8), 4); |
| 81 | |
| 82 | delete out; |
| 83 | |
| 84 | driver->WriteData(buffer, 44); |
| 85 | |
| 86 | return True; |
| 87 | } |
| 88 | |
| 89 | Bool BoCA::EncoderWave::Deactivate() |
| 90 | { |
| 91 | static Endianness endianness = CPU().GetEndianness(); |
| 92 | |
| 93 | const Config *config = GetConfiguration(); |
| 94 | const Info &info = track.GetInfo(); |
| 95 | |
| 96 | /* Write data size to header. |
| 97 | */ |
| 98 | UnsignedInt dataSize = driver->GetSize() - 44; |
| 99 | |
| 100 | driver->Seek(40); |
| 101 | |
| 102 | if (endianness == EndianLittle) for (Int i = 0; i <= 3; i++) driver->WriteData(((unsigned char *) &dataSize) + i, 1); |
| 103 | else for (Int i = 3; i >= 0; i--) driver->WriteData(((unsigned char *) &dataSize) + i, 1); |
| 104 | |
| 105 | driver->Seek(driver->GetSize()); |
| 106 | |
| 107 | /* Write RIFF tag if requested. |
| 108 | */ |
| 109 | if (config->GetIntValue("Tags", "EnableRIFFINFOTag", True) && info.HasBasicInfo()) |
| 110 | { |
| 111 | AS::Registry &boca = AS::Registry::Get(); |
| 112 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("riff-tag"); |
| 113 | |
| 114 | if (tagger != NIL) |
| 115 | { |
| 116 | Buffer<unsigned char> tagBuffer; |
| 117 | |
| 118 | tagger->SetConfiguration(GetConfiguration()); |
| 119 | tagger->RenderBuffer(tagBuffer, track); |
| 120 | |
| 121 | driver->WriteData(tagBuffer, tagBuffer.Size()); |
| 122 | |
| 123 | boca.DeleteComponent(tagger); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* Write CART tag if requested. |
| 128 | */ |
| 129 | if (config->GetIntValue("Tags", "EnableRIFFCartTag", True) && info.HasBasicInfo()) |
| 130 | { |
| 131 | AS::Registry &boca = AS::Registry::Get(); |
| 132 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("cart-tag"); |
| 133 | |
| 134 | if (tagger != NIL) |
| 135 | { |
| 136 | Buffer<unsigned char> tagBuffer; |
| 137 | |
| 138 | tagger->SetConfiguration(GetConfiguration()); |
| 139 | tagger->RenderBuffer(tagBuffer, track); |
| 140 | |
| 141 | driver->WriteData(tagBuffer, tagBuffer.Size()); |
| 142 | |
| 143 | boca.DeleteComponent(tagger); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Write ID3v2 tag if requested. |
| 148 | */ |
| 149 | if (config->GetIntValue("Tags", "EnableID3v2", True) && (info.HasBasicInfo() || (track.tracks.Length() > 0 && config->GetIntValue("Tags", "WriteChapters", True)))) |
| 150 | { |
| 151 | AS::Registry &boca = AS::Registry::Get(); |
| 152 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 153 | |
| 154 | if (tagger != NIL) |
| 155 | { |
| 156 | Buffer<unsigned char> id3Buffer; |
| 157 | |
| 158 | tagger->SetConfiguration(config); |
| 159 | tagger->RenderBuffer(id3Buffer, track); |
| 160 | |
| 161 | driver->WriteData((unsigned char *) "id3 ", 4); |
| 162 | |
| 163 | Int size = id3Buffer.Size(); |
| 164 | |
| 165 | if (endianness == EndianLittle) for (Int i = 0; i <= 3; i++) driver->WriteData(((unsigned char *) &size) + i, 1); |
| 166 | else for (Int i = 3; i >= 0; i--) driver->WriteData(((unsigned char *) &size) + i, 1); |
| 167 | |
| 168 | driver->WriteData(id3Buffer, id3Buffer.Size()); |
| 169 | |
| 170 | boca.DeleteComponent(tagger); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Write file size to header. |
| 175 | */ |
| 176 | UnsignedInt fileSize = driver->GetSize() - 8; |
| 177 | |
| 178 | driver->Seek(4); |
| 179 | |
| 180 | if (endianness == EndianLittle) for (Int i = 0; i <= 3; i++) driver->WriteData(((unsigned char *) &fileSize) + i, 1); |
| 181 | else for (Int i = 3; i >= 0; i--) driver->WriteData(((unsigned char *) &fileSize) + i, 1); |
| 182 | |
| 183 | driver->Seek(driver->GetSize()); |
| 184 | |
| 185 | return True; |
| 186 | } |
| 187 | |
| 188 | Int BoCA::EncoderWave::WriteData(Buffer<UnsignedByte> &data) |
| 189 | { |
| 190 | static Endianness endianness = CPU().GetEndianness(); |
| 191 | |
| 192 | if (endianness != EndianLittle) BoCA::Utilities::SwitchBufferByteOrder(data, track.GetFormat().bits / 8); |
| 193 | |
| 194 | return driver->WriteData(data, data.Size()); |
| 195 | } |