| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2021 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 "cdtext.h" |
| 14 | #include "../dllinterface.h" |
| 15 | |
| 16 | typedef struct |
| 17 | { |
| 18 | unsigned char packType; |
| 19 | unsigned char trackNumber; |
| 20 | unsigned char sequenceNumber; |
| 21 | |
| 22 | unsigned char characterPosition :4; |
| 23 | unsigned char block :3; |
| 24 | unsigned char bDBC :1; |
| 25 | |
| 26 | unsigned char data[12]; |
| 27 | unsigned char crc0; |
| 28 | unsigned char crc1; |
| 29 | } |
| 30 | cdTextPackage; |
| 31 | |
| 32 | static const String cdTextGenre[28] = |
| 33 | { "Not Used", "Not Defined", "Adult Contemporary", "Alternative Rock", "Childrens Music", |
| 34 | "Classical", "Contemporary Christian", "Country", "Dance", "Easy Listening", "Erotic", "Folk", |
| 35 | "Gospel", "Hip Hop", "Jazz", "Latin", "Musical", "New Age", "Opera", "Operetta", "Pop Music", |
| 36 | "Rap", "Reggae", "Rock Music", "Rhythm & Blues", "Sound Effects", "Spoken Word", "World Music" }; |
| 37 | |
| 38 | BoCA::CDText::CDText() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | BoCA::CDText::~CDText() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | Int BoCA::CDText::ReadCDText(Int drive) |
| 47 | { |
| 48 | CDROMDRIVE *cd = ex_CR_OpenCDROM(drive); |
| 49 | |
| 50 | if (cd == NIL) return Error(); |
| 51 | |
| 52 | cdInfo.Clear(); |
| 53 | |
| 54 | /* Read CD-Text. |
| 55 | */ |
| 56 | Buffer<unsigned char> cdTextBuffer(4 + 8 * sizeof(cdTextPackage) * 256); |
| 57 | Int cdTextSize = 0; |
| 58 | |
| 59 | ex_CR_ReadCDText(cd, cdTextBuffer, cdTextBuffer.Size(), &cdTextSize); |
| 60 | |
| 61 | if (cdTextSize < 4) { ex_CR_CloseCDROM(cd); return Error(); } |
| 62 | |
| 63 | /* Process CD-Text packs. |
| 64 | */ |
| 65 | Int numPacks = (cdTextSize - 4) / sizeof(cdTextPackage); |
| 66 | cdTextPackage *currentPack = NIL; |
| 67 | |
| 68 | String data; |
| 69 | char dataBuffer[3036] = { 0 }; |
| 70 | Int dataBufferPos = 0; |
| 71 | Int genreCode = 0; |
| 72 | |
| 73 | for (Int i = 0; i < numPacks; i++) |
| 74 | { |
| 75 | currentPack = (cdTextPackage *) &cdTextBuffer[i * sizeof(cdTextPackage) + 4]; |
| 76 | |
| 77 | if (currentPack->block != 0) continue; |
| 78 | |
| 79 | String::InputFormat format("ISO-8859-1"); |
| 80 | |
| 81 | String tabString = "\t"; |
| 82 | Int nullBytes = 1; |
| 83 | |
| 84 | if (currentPack->bDBC) |
| 85 | { |
| 86 | String::SetInputFormat("Shift_JIS"); |
| 87 | |
| 88 | tabString = "\t\t"; |
| 89 | nullBytes = 2; |
| 90 | } |
| 91 | |
| 92 | /* Append pack data to buffer. |
| 93 | */ |
| 94 | for (Int j = 0; j < 12; j++) |
| 95 | { |
| 96 | if (j == 0 && currentPack->packType == 0x87 && currentPack->data[0] < 31 && currentPack->data[1] < 31) // Genre code |
| 97 | { |
| 98 | if (currentPack->data[0] == 0) genreCode = currentPack->data[0] << 8 | currentPack->data[1]; |
| 99 | else genreCode = currentPack->data[1] << 8 | currentPack->data[0]; |
| 100 | |
| 101 | j++; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | dataBuffer[dataBufferPos++] = currentPack->data[j]; |
| 106 | } |
| 107 | |
| 108 | /* Process pack data. |
| 109 | */ |
| 110 | if (String(dataBuffer) != tabString) data = ReplaceFullWidthChars(dataBuffer); |
| 111 | |
| 112 | while (dataBufferPos > 0) |
| 113 | { |
| 114 | /* Update text data. |
| 115 | */ |
| 116 | if (currentPack->packType == 0x80) // Album/Track title |
| 117 | { |
| 118 | if (currentPack->trackNumber == 0) cdInfo.SetTitle(data); |
| 119 | else cdInfo.SetTrackTitle(currentPack->trackNumber, data); |
| 120 | } |
| 121 | else if (currentPack->packType == 0x81) // Artist name |
| 122 | { |
| 123 | if (currentPack->trackNumber == 0) cdInfo.SetArtist(data); |
| 124 | else cdInfo.SetTrackArtist(currentPack->trackNumber, data); |
| 125 | } |
| 126 | else if (currentPack->packType == 0x82) // Songwriter |
| 127 | { |
| 128 | if (currentPack->trackNumber == 0) cdInfo.SetSongwriter(data); |
| 129 | else cdInfo.SetTrackSongwriter(currentPack->trackNumber, data); |
| 130 | } |
| 131 | else if (currentPack->packType == 0x83) // Composer |
| 132 | { |
| 133 | if (currentPack->trackNumber == 0) cdInfo.SetComposer(data); |
| 134 | else cdInfo.SetTrackComposer(currentPack->trackNumber, data); |
| 135 | } |
| 136 | else if (currentPack->packType == 0x84) // Arranger |
| 137 | { |
| 138 | if (currentPack->trackNumber == 0) cdInfo.SetArranger(data); |
| 139 | else cdInfo.SetTrackArranger(currentPack->trackNumber, data); |
| 140 | } |
| 141 | else if (currentPack->packType == 0x85) // Message |
| 142 | { |
| 143 | if (currentPack->trackNumber == 0) cdInfo.SetComment(data); |
| 144 | else cdInfo.SetTrackComment(currentPack->trackNumber, data); |
| 145 | } |
| 146 | else if (currentPack->packType == 0x86) // Catalog number |
| 147 | { |
| 148 | cdInfo.SetCatalog(data); |
| 149 | } |
| 150 | else if (currentPack->packType == 0x87) // Genre |
| 151 | { |
| 152 | cdInfo.SetGenre(data); |
| 153 | } |
| 154 | else if (currentPack->packType == 0x8e) // UPC/EAN and ISRC |
| 155 | { |
| 156 | if (currentPack->trackNumber == 0) cdInfo.SetBarcode(data); |
| 157 | else cdInfo.SetTrackISRC(currentPack->trackNumber, data); |
| 158 | } |
| 159 | |
| 160 | /* Look for string terminator. |
| 161 | */ |
| 162 | char *lpZero = (char *) memchr(dataBuffer, 0, dataBufferPos); |
| 163 | |
| 164 | if (lpZero == NIL) break; |
| 165 | |
| 166 | /* Shift buffer if terminated. |
| 167 | */ |
| 168 | Int nRemove = (lpZero - dataBuffer) + nullBytes; |
| 169 | |
| 170 | memmove(dataBuffer, dataBuffer + nRemove, sizeof(dataBuffer) - nRemove); |
| 171 | dataBufferPos -= nRemove; |
| 172 | |
| 173 | currentPack->trackNumber++; |
| 174 | |
| 175 | /* Skip zero bytes. |
| 176 | */ |
| 177 | while (dataBufferPos >= nullBytes && dataBuffer[0] == 0) |
| 178 | { |
| 179 | memmove(dataBuffer, dataBuffer + nullBytes, sizeof(dataBuffer) - nullBytes); |
| 180 | dataBufferPos -= nullBytes; |
| 181 | |
| 182 | currentPack->trackNumber++; |
| 183 | } |
| 184 | |
| 185 | /* Update data string. |
| 186 | */ |
| 187 | if (dataBufferPos && String(dataBuffer) != tabString) data = ReplaceFullWidthChars(dataBuffer); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | ex_CR_CloseCDROM(cd); |
| 192 | |
| 193 | if (cdInfo.GetGenre().Trim() == NIL && genreCode != 0 && genreCode < 28) cdInfo.SetGenre(cdTextGenre[genreCode]); |
| 194 | |
| 195 | return Success(); |
| 196 | } |
| 197 | |
| 198 | const BoCA::CDInfo &BoCA::CDText::GetCDInfo() const |
| 199 | { |
| 200 | return cdInfo; |
| 201 | } |
| 202 | |
| 203 | String BoCA::CDText::ReplaceFullWidthChars(const String &string) |
| 204 | { |
| 205 | String result = string; |
| 206 | Int length = string.Length(); |
| 207 | |
| 208 | for (Int i = 0; i < length; i++) |
| 209 | { |
| 210 | if (result[i] >= 0xff01 && result[i] <= 0xff5e) result[i] -= 0xfee0; |
| 211 | else if (result[i] == 0x3000 ) result[i] = 0x20; |
| 212 | } |
| 213 | |
| 214 | return result; |
| 215 | } |