| 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 <smooth.h> |
| 14 | #include <smooth/dll.h> |
| 15 | |
| 16 | #include "bonk.h" |
| 17 | |
| 18 | using namespace smooth::IO; |
| 19 | |
| 20 | const String &BoCA::DecoderBonk::GetComponentSpecs() |
| 21 | { |
| 22 | static String componentSpecs; |
| 23 | |
| 24 | if (bonkdll != NIL) |
| 25 | { |
| 26 | componentSpecs = " \ |
| 27 | \ |
| 28 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 29 | <component> \ |
| 30 | <name>Bonk Audio Decoder %VERSION%</name> \ |
| 31 | <version>1.0</version> \ |
| 32 | <id>bonk-dec</id> \ |
| 33 | <type>decoder</type> \ |
| 34 | <format> \ |
| 35 | <name>Bonk Audio Files</name> \ |
| 36 | <extension>bonk</extension> \ |
| 37 | </format> \ |
| 38 | </component> \ |
| 39 | \ |
| 40 | "; |
| 41 | |
| 42 | componentSpecs.Replace("%VERSION%", String("v").Append(ex_bonk_get_version_string())); |
| 43 | } |
| 44 | |
| 45 | return componentSpecs; |
| 46 | } |
| 47 | |
| 48 | Void smooth::AttachDLL(Void *instance) |
| 49 | { |
| 50 | LoadBonkDLL(); |
| 51 | } |
| 52 | |
| 53 | Void smooth::DetachDLL() |
| 54 | { |
| 55 | FreeBonkDLL(); |
| 56 | } |
| 57 | |
| 58 | Bool BoCA::DecoderBonk::CanOpenStream(const String &streamURI) |
| 59 | { |
| 60 | return streamURI.ToLower().EndsWith(".bonk"); |
| 61 | } |
| 62 | |
| 63 | Error BoCA::DecoderBonk::GetStreamInfo(const String &streamURI, Track &track) |
| 64 | { |
| 65 | InStream *in = new InStream(STREAM_FILE, streamURI, IS_READ); |
| 66 | unsigned int length = 0; |
| 67 | unsigned int rate = 0; |
| 68 | int channels = 0; |
| 69 | void *decoder = ex_bonk_decoder_create(); |
| 70 | |
| 71 | Format format = track.GetFormat(); |
| 72 | |
| 73 | Buffer<UnsignedByte> data(Math::Min(in->Size(), (Int64) 524288)); |
| 74 | |
| 75 | in->InputData(data, data.Size()); |
| 76 | |
| 77 | ex_bonk_decoder_init(decoder, data, data.Size(), &length, &rate, &channels); |
| 78 | |
| 79 | format.rate = rate; |
| 80 | format.channels = channels; |
| 81 | format.bits = 16; |
| 82 | |
| 83 | track.SetFormat(format); |
| 84 | |
| 85 | track.length = length / format.channels; |
| 86 | track.fileSize = in->Size(); |
| 87 | |
| 88 | bool lossless = False; |
| 89 | bool mid_side = False; |
| 90 | |
| 91 | ex_bonk_decoder_get_stream_info(decoder, &lossless, &mid_side); |
| 92 | |
| 93 | track.lossless = lossless; |
| 94 | |
| 95 | unsigned char *id3tag = NIL; |
| 96 | int id3tag_size = 0; |
| 97 | |
| 98 | ex_bonk_decoder_get_id3_data(decoder, &id3tag, &id3tag_size); |
| 99 | |
| 100 | if (id3tag_size > 0) |
| 101 | { |
| 102 | Buffer<unsigned char> buffer(id3tag_size); |
| 103 | |
| 104 | memcpy(buffer, id3tag, id3tag_size); |
| 105 | |
| 106 | AS::Registry &boca = AS::Registry::Get(); |
| 107 | AS::TaggerComponent *tagger = (AS::TaggerComponent *) boca.CreateComponentByID("id3v2-tag"); |
| 108 | |
| 109 | if (tagger != NIL) |
| 110 | { |
| 111 | tagger->SetConfiguration(GetConfiguration()); |
| 112 | tagger->ParseBuffer(buffer, track); |
| 113 | |
| 114 | boca.DeleteComponent(tagger); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | ex_bonk_decoder_close(decoder); |
| 119 | |
| 120 | delete in; |
| 121 | |
| 122 | return Success(); |
| 123 | } |
| 124 | |
| 125 | BoCA::DecoderBonk::DecoderBonk() |
| 126 | { |
| 127 | decoder = NIL; |
| 128 | } |
| 129 | |
| 130 | BoCA::DecoderBonk::~DecoderBonk() |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | Bool BoCA::DecoderBonk::Activate() |
| 135 | { |
| 136 | unsigned int length = 0; |
| 137 | unsigned int rate = 0; |
| 138 | int channels = 0; |
| 139 | |
| 140 | decoder = ex_bonk_decoder_create(); |
| 141 | |
| 142 | Buffer<UnsignedByte> data(Math::Min(driver->GetSize(), (Int64) 524288)); |
| 143 | |
| 144 | driver->ReadData(data, data.Size()); |
| 145 | |
| 146 | ex_bonk_decoder_init(decoder, data, data.Size(), &length, &rate, &channels); |
| 147 | |
| 148 | return True; |
| 149 | } |
| 150 | |
| 151 | Bool BoCA::DecoderBonk::Deactivate() |
| 152 | { |
| 153 | ex_bonk_decoder_finish(decoder); |
| 154 | |
| 155 | ex_bonk_decoder_close(decoder); |
| 156 | |
| 157 | return True; |
| 158 | } |
| 159 | |
| 160 | Int BoCA::DecoderBonk::ReadData(Buffer<UnsignedByte> &data) |
| 161 | { |
| 162 | Int size = driver->ReadData(data, data.Size()); |
| 163 | |
| 164 | data.Resize(Math::Max(data.Size(), 131072)); |
| 165 | |
| 166 | Int nSamples = ex_bonk_decoder_decode_packet(decoder, data, Math::Max(0, size), (signed short *) (UnsignedByte *) data, data.Size()); |
| 167 | |
| 168 | if (nSamples == -1) return -1; |
| 169 | |
| 170 | data.Resize(nSamples * (track.GetFormat().bits / 8)); |
| 171 | |
| 172 | return data.Size(); |
| 173 | } |