| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2020 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_IEEE_FLOAT 0x0003 |
| 19 | # define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
| 20 | #endif |
| 21 | |
| 22 | #include "wave.h" |
| 23 | |
| 24 | using namespace smooth::IO; |
| 25 | |
| 26 | const String &BoCA::DecoderWave::GetComponentSpecs() |
| 27 | { |
| 28 | static String componentSpecs = " \ |
| 29 | \ |
| 30 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 31 | <component> \ |
| 32 | <name>Windows Wave File Input</name> \ |
| 33 | <version>1.0</version> \ |
| 34 | <id>wave-dec</id> \ |
| 35 | <type>decoder</type> \ |
| 36 | <format> \ |
| 37 | <name>Microsoft Wave Files</name> \ |
| 38 | <lossless>true</lossless> \ |
| 39 | <extension>wav</extension> \ |
| 40 | <tag id=\"riff-tag\" mode=\"other\">RIFF INFO Tag</tag> \ |
| 41 | <tag id=\"cart-tag\" mode=\"other\">RIFF Cart Tag</tag> \ |
| 42 | <tag id=\"id3v2-tag\" mode=\"other\">ID3v2</tag> \ |
| 43 | </format> \ |
| 44 | </component> \ |
| 45 | \ |
| 46 | "; |
| 47 | |
| 48 | return componentSpecs; |
| 49 | } |
| 50 | |
| 51 | Bool BoCA::DecoderWave::CanOpenStream(const String &streamURI) |
| 52 | { |
| 53 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 54 | |
| 55 | if (in.InputString(4) != "RIFF") return False; |
| 56 | |
| 57 | in.RelSeek(4); |
| 58 | |
| 59 | if (in.InputString(4) != "WAVE") return False; |
| 60 | |
| 61 | String chunk; |
| 62 | |
| 63 | while (chunk != "fmt ") |
| 64 | { |
| 65 | if (in.GetPos() >= in.Size()) break; |
| 66 | |
| 67 | /* Read next chunk. |
| 68 | */ |
| 69 | chunk = in.InputString(4); |
| 70 | |
| 71 | UnsignedInt32 cSize = in.InputNumber(4); |
| 72 | |
| 73 | if (chunk == "fmt ") |
| 74 | { |
| 75 | Int waveFormat = in.InputNumber(2); |
| 76 | |
| 77 | if (waveFormat == WAVE_FORMAT_PCM || |
| 78 | waveFormat == WAVE_FORMAT_IEEE_FLOAT || |
| 79 | waveFormat == WAVE_FORMAT_EXTENSIBLE) return True; |
| 80 | |
| 81 | /* Skip rest of chunk. |
| 82 | */ |
| 83 | if (!in.RelSeek(cSize - 2 + cSize % 2)) break; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | /* Skip chunk. |
| 88 | */ |
| 89 | if (!in.RelSeek(cSize + cSize % 2)) break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return False; |
| 94 | } |
| 95 | |
| 96 | Error BoCA::DecoderWave::GetStreamInfo(const String &streamURI, Track &track) |
| 97 | { |
| 98 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 99 | |
| 100 | track.fileSize = in.Size(); |
| 101 | |
| 102 | /* Read RIFF chunk. |
| 103 | */ |
| 104 | if (in.InputString(4) != "RIFF") { errorState = True; errorString = "Unknown file type"; } |
| 105 | |
| 106 | UnsignedInt32 rSize = in.InputNumber(4); |
| 107 | |
| 108 | if (in.InputString(4) != "WAVE") { errorState = True; errorString = "Unknown file type"; } |
| 109 | |
| 110 | String chunk; |
| 111 | |
| 112 | while (!errorState) |
| 113 | { |
| 114 | if (in.GetPos() >= in.Size()) break; |
| 115 | |
| 116 | /* Read next chunk. |
| 117 | */ |
| 118 | chunk = in.InputString(4); |
| 119 | |
| 120 | UnsignedInt64 cSize = (UnsignedInt32) in.InputNumber(4); |
| 121 | |
| 122 | if (chunk == "fmt ") |
| 123 | { |
| 124 | Int waveFormat = in.InputNumber(2); |
| 125 | |
| 126 | if (waveFormat != WAVE_FORMAT_PCM && |
| 127 | waveFormat != WAVE_FORMAT_IEEE_FLOAT && |
| 128 | waveFormat != WAVE_FORMAT_EXTENSIBLE) { errorState = True; errorString = "Unsupported audio format"; } |
| 129 | |
| 130 | Format format = track.GetFormat(); |
| 131 | |
| 132 | format.channels = (unsigned short) in.InputNumber(2); |
| 133 | format.rate = (unsigned long) in.InputNumber(4); |
| 134 | |
| 135 | in.RelSeek(6); |
| 136 | |
| 137 | format.fp = (waveFormat == WAVE_FORMAT_IEEE_FLOAT); |
| 138 | format.bits = (unsigned short) in.InputNumber(2); |
| 139 | format.order = (waveFormat == WAVE_FORMAT_IEEE_FLOAT && format.bits == 64) ? BYTE_NATIVE : BYTE_INTEL; |
| 140 | |
| 141 | if (format.bits == 8) format.sign = False; |
| 142 | |
| 143 | track.SetFormat(format); |
| 144 | |
| 145 | /* Skip rest of chunk. |
| 146 | */ |
| 147 | in.RelSeek(cSize - 16 + cSize % 2); |
| 148 | } |
| 149 | else if (chunk == "data") |
| 150 | { |
| 151 | Format format = track.GetFormat(); |
| 152 | |
| 153 | if (rSize == 0xFFFFFFFF || rSize == 0 || |
| 154 | cSize == 0xFFFFFFFF || cSize == 0) cSize = in.Size() - in.GetPos(); |
| 155 | |
| 156 | track.length = cSize / format.channels / (format.bits / 8); |
| 157 | format.bits = Math::Min(32, format.bits); |
| 158 | |
| 159 | track.SetFormat(format); |
| 160 | |
| 161 | /* Skip rest of chunk. |
| 162 | */ |
| 163 | in.RelSeek(cSize + cSize % 2); |
| 164 | } |
| 165 | else if (chunk == "id3 ") |
| 166 | { |
| 167 | Buffer<UnsignedByte> buffer(cSize); |
| 168 | |
| 169 | in.InputData(buffer, cSize); |
| 170 | |
| 171 | AS::Registry &boca = AS::Registry::Get(); |
| 172 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 173 | |
| 174 | if (tagger != NIL) |
| 175 | { |
| 176 | tagger->SetConfiguration(GetConfiguration()); |
| 177 | tagger->ParseBuffer(buffer, track); |
| 178 | |
| 179 | boca.DeleteComponent(tagger); |
| 180 | } |
| 181 | |
| 182 | /* Skip rest of chunk. |
| 183 | */ |
| 184 | in.RelSeek(cSize % 2); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | /* Skip chunk. |
| 189 | */ |
| 190 | if (!in.RelSeek(cSize + cSize % 2)) { errorState = True; errorString = "Invalid file format"; } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (!errorState) |
| 195 | { |
| 196 | AS::Registry &boca = AS::Registry::Get(); |
| 197 | AS::TaggerComponent *cartTagger = (AS::TaggerComponent *) boca.CreateComponentByID("cart-tag"); |
| 198 | |
| 199 | if (cartTagger != NIL) |
| 200 | { |
| 201 | cartTagger->SetConfiguration(GetConfiguration()); |
| 202 | cartTagger->ParseStreamInfo(streamURI, track); |
| 203 | |
| 204 | boca.DeleteComponent(cartTagger); |
| 205 | } |
| 206 | |
| 207 | AS::TaggerComponent *riffTagger = (AS::TaggerComponent *) boca.CreateComponentByID("riff-tag"); |
| 208 | |
| 209 | if (riffTagger != NIL) |
| 210 | { |
| 211 | riffTagger->SetConfiguration(GetConfiguration()); |
| 212 | riffTagger->ParseStreamInfo(streamURI, track); |
| 213 | |
| 214 | boca.DeleteComponent(riffTagger); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (errorState) return Error(); |
| 219 | else return Success(); |
| 220 | } |
| 221 | |
| 222 | BoCA::DecoderWave::DecoderWave() |
| 223 | { |
| 224 | floatFormat = False; |
| 225 | floatFormatBits = 32; |
| 226 | |
| 227 | dataOffset = 0; |
| 228 | } |
| 229 | |
| 230 | BoCA::DecoderWave::~DecoderWave() |
| 231 | { |
| 232 | } |
| 233 | |
| 234 | Bool BoCA::DecoderWave::Activate() |
| 235 | { |
| 236 | InStream in(STREAM_DRIVER, driver); |
| 237 | |
| 238 | in.Seek(12); |
| 239 | |
| 240 | String chunk; |
| 241 | |
| 242 | do |
| 243 | { |
| 244 | /* Read next chunk. |
| 245 | */ |
| 246 | chunk = in.InputString(4); |
| 247 | |
| 248 | UnsignedInt32 cSize = in.InputNumber(4); |
| 249 | |
| 250 | if (chunk == "fmt ") |
| 251 | { |
| 252 | Int waveFormat = in.InputNumber(2); |
| 253 | |
| 254 | if (waveFormat == WAVE_FORMAT_IEEE_FLOAT) floatFormat = True; |
| 255 | |
| 256 | in.RelSeek(12); |
| 257 | |
| 258 | floatFormatBits = (unsigned short) in.InputNumber(2); |
| 259 | |
| 260 | /* Skip rest of chunk. |
| 261 | */ |
| 262 | in.RelSeek(cSize - 16 + cSize % 2); |
| 263 | } |
| 264 | else if (chunk != "data") |
| 265 | { |
| 266 | if (!in.RelSeek(cSize + cSize % 2)) return False; |
| 267 | } |
| 268 | } |
| 269 | while (chunk != "data"); |
| 270 | |
| 271 | dataOffset = in.GetPos(); |
| 272 | |
| 273 | driver->Seek(dataOffset); |
| 274 | |
| 275 | return True; |
| 276 | } |
| 277 | |
| 278 | Bool BoCA::DecoderWave::Seek(Int64 samplePosition) |
| 279 | { |
| 280 | const Format &format = track.GetFormat(); |
| 281 | |
| 282 | driver->Seek(dataOffset + samplePosition * format.channels * (format.bits / 8) * (floatFormatBits / 32)); |
| 283 | |
| 284 | return True; |
| 285 | } |
| 286 | |
| 287 | Int BoCA::DecoderWave::ReadData(Buffer<UnsignedByte> &data) |
| 288 | { |
| 289 | static Endianness endianness = CPU().GetEndianness(); |
| 290 | |
| 291 | if (driver->GetPos() == driver->GetSize()) return -1; |
| 292 | |
| 293 | /* Read data. |
| 294 | */ |
| 295 | if (floatFormat && floatFormatBits == 64) data.Resize(data.Size() * 2); |
| 296 | |
| 297 | Int size = driver->ReadData(data, data.Size()); |
| 298 | |
| 299 | /* Convert 64 bit float to 32 bit. |
| 300 | */ |
| 301 | if (floatFormat && floatFormatBits == 64) |
| 302 | { |
| 303 | if (endianness != EndianLittle) BoCA::Utilities::SwitchBufferByteOrder(data, 8); |
| 304 | |
| 305 | for (Int i = 0; i < size / 8; i++) |
| 306 | { |
| 307 | Float64 sample64 = ((Float64 *) (unsigned char *) data)[i]; |
| 308 | Float32 &sample32 = ((Float32 *) (unsigned char *) data)[i]; |
| 309 | |
| 310 | sample32 = sample64; |
| 311 | } |
| 312 | |
| 313 | size /= 2; |
| 314 | |
| 315 | data.Resize(size); |
| 316 | } |
| 317 | |
| 318 | return size; |
| 319 | } |