| 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 | #include "voc.h" |
| 14 | |
| 15 | using namespace smooth::IO; |
| 16 | |
| 17 | const String &BoCA::DecoderVoc::GetComponentSpecs() |
| 18 | { |
| 19 | static String componentSpecs = " \ |
| 20 | \ |
| 21 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 22 | <component> \ |
| 23 | <name>Creative Voice File Decoder</name> \ |
| 24 | <version>1.0</version> \ |
| 25 | <id>voc-dec</id> \ |
| 26 | <type>decoder</type> \ |
| 27 | <format> \ |
| 28 | <name>Creative Voice Files</name> \ |
| 29 | <lossless>true</lossless> \ |
| 30 | <extension>voc</extension> \ |
| 31 | </format> \ |
| 32 | </component> \ |
| 33 | \ |
| 34 | "; |
| 35 | |
| 36 | return componentSpecs; |
| 37 | } |
| 38 | |
| 39 | Bool BoCA::DecoderVoc::CanOpenStream(const String &streamURI) |
| 40 | { |
| 41 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 42 | |
| 43 | if (in.InputString(19) == "Creative Voice File" && in.InputNumber(1) == 0x1A) |
| 44 | { |
| 45 | /* Skip rest of header. |
| 46 | */ |
| 47 | in.RelSeek(6); |
| 48 | |
| 49 | /* Read block type. |
| 50 | */ |
| 51 | Int blockType = in.InputNumber(1); |
| 52 | |
| 53 | if (blockType == 0x09) return True; |
| 54 | } |
| 55 | |
| 56 | return False; |
| 57 | } |
| 58 | |
| 59 | Error BoCA::DecoderVoc::GetStreamInfo(const String &streamURI, Track &track) |
| 60 | { |
| 61 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 62 | |
| 63 | track.fileSize = in.Size(); |
| 64 | |
| 65 | /* Skip main header. |
| 66 | */ |
| 67 | in.RelSeek(26); |
| 68 | |
| 69 | /* Read block type. |
| 70 | */ |
| 71 | Int blockType = in.InputNumber(1); |
| 72 | |
| 73 | if (blockType == 0x09) |
| 74 | { |
| 75 | /* Read block size. |
| 76 | */ |
| 77 | Int blockSize = in.InputNumber(3); |
| 78 | |
| 79 | /* Read format data. |
| 80 | */ |
| 81 | Format format = track.GetFormat(); |
| 82 | |
| 83 | format.order = BYTE_INTEL; |
| 84 | format.rate = UnsignedInt32(in.InputNumber(4)); |
| 85 | format.bits = UnsignedInt8(in.InputNumber(1)); |
| 86 | format.channels = UnsignedInt8(in.InputNumber(1)); |
| 87 | |
| 88 | if (format.bits == 8) format.sign = False; |
| 89 | |
| 90 | track.SetFormat(format); |
| 91 | |
| 92 | in.RelSeek(6); |
| 93 | in.RelSeek(blockSize - 12); |
| 94 | |
| 95 | /* Get track length. |
| 96 | */ |
| 97 | track.length = (blockSize - 12) / format.channels / (format.bits / 8); |
| 98 | |
| 99 | if (in.InputNumber(1) == 0x02) |
| 100 | { |
| 101 | do |
| 102 | { |
| 103 | Int blockSize = in.InputNumber(3); |
| 104 | |
| 105 | track.length += blockSize / format.channels / (format.bits / 8); |
| 106 | |
| 107 | in.RelSeek(blockSize); |
| 108 | } |
| 109 | while (in.InputNumber(1) == 0x02); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | track.length = (track.fileSize - 42) / format.channels / (format.bits / 8); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return Success(); |
| 118 | } |
| 119 | |
| 120 | BoCA::DecoderVoc::DecoderVoc() |
| 121 | { |
| 122 | bytesLeft = 0; |
| 123 | } |
| 124 | |
| 125 | BoCA::DecoderVoc::~DecoderVoc() |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | Bool BoCA::DecoderVoc::Activate() |
| 130 | { |
| 131 | InStream in(STREAM_DRIVER, driver); |
| 132 | |
| 133 | /* Skip main header. |
| 134 | */ |
| 135 | in.RelSeek(26); |
| 136 | |
| 137 | /* Read block type. |
| 138 | */ |
| 139 | Int blockType = in.InputNumber(1); |
| 140 | |
| 141 | if (blockType == 0x09) |
| 142 | { |
| 143 | /* Read block size and continue. |
| 144 | */ |
| 145 | bytesLeft = in.InputNumber(3) - 12; |
| 146 | |
| 147 | driver->Seek(in.GetPos() + 12); |
| 148 | } |
| 149 | |
| 150 | return True; |
| 151 | } |
| 152 | |
| 153 | Int BoCA::DecoderVoc::ReadData(Buffer<UnsignedByte> &data) |
| 154 | { |
| 155 | Int size = driver->ReadData(data, data.Size()); |
| 156 | |
| 157 | if (size <= 0) return -1; |
| 158 | |
| 159 | Int outSize = size; |
| 160 | |
| 161 | if (size > bytesLeft) |
| 162 | { |
| 163 | if ((data + bytesLeft)[0] == 0x02) |
| 164 | { |
| 165 | Int newBytesLeft = (data + bytesLeft)[1] + |
| 166 | 256 * (data + bytesLeft)[2] + |
| 167 | 65536 * (data + bytesLeft)[3]; |
| 168 | |
| 169 | outSize = size - 4; |
| 170 | |
| 171 | memmove(data + bytesLeft, data + bytesLeft + 4, size - bytesLeft - 4); |
| 172 | |
| 173 | data.Resize(outSize); |
| 174 | |
| 175 | bytesLeft = newBytesLeft - (size - bytesLeft - 4); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | /* Possibly broken format. |
| 180 | */ |
| 181 | bytesLeft = driver->GetSize(); |
| 182 | } |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | bytesLeft -= size; |
| 187 | } |
| 188 | |
| 189 | return outSize; |
| 190 | } |