| 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 "au.h" |
| 14 | |
| 15 | using namespace smooth::IO; |
| 16 | |
| 17 | const String &BoCA::DecoderSunAu::GetComponentSpecs() |
| 18 | { |
| 19 | static String componentSpecs = " \ |
| 20 | \ |
| 21 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 22 | <component> \ |
| 23 | <name>Sun Audio File Decoder</name> \ |
| 24 | <version>1.0</version> \ |
| 25 | <id>au-dec</id> \ |
| 26 | <type>decoder</type> \ |
| 27 | <format> \ |
| 28 | <name>Sun Audio Files</name> \ |
| 29 | <lossless>true</lossless> \ |
| 30 | <extension>au</extension> \ |
| 31 | <extension>snd</extension> \ |
| 32 | </format> \ |
| 33 | </component> \ |
| 34 | \ |
| 35 | "; |
| 36 | |
| 37 | return componentSpecs; |
| 38 | } |
| 39 | |
| 40 | Bool BoCA::DecoderSunAu::CanOpenStream(const String &streamURI) |
| 41 | { |
| 42 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 43 | |
| 44 | if (in.InputString(4) == ".snd") return True; |
| 45 | |
| 46 | return False; |
| 47 | } |
| 48 | |
| 49 | Error BoCA::DecoderSunAu::GetStreamInfo(const String &streamURI, Track &track) |
| 50 | { |
| 51 | InStream in(STREAM_FILE, streamURI, IS_READ); |
| 52 | Format format = track.GetFormat(); |
| 53 | |
| 54 | track.fileSize = in.Size(); |
| 55 | format.order = BYTE_RAW; |
| 56 | |
| 57 | /* Skip magic number and data offset. |
| 58 | */ |
| 59 | in.RelSeek(8); |
| 60 | |
| 61 | track.length = UnsignedInt32(in.InputNumberRaw(4)); |
| 62 | format.bits = UnsignedInt32(in.InputNumberRaw(4)); |
| 63 | |
| 64 | if (format.bits == 2) format.bits = 8; |
| 65 | else if (format.bits == 3) format.bits = 16; |
| 66 | else if (format.bits == 4) format.bits = 24; |
| 67 | else if (format.bits == 5) format.bits = 32; |
| 68 | else { errorState = True; errorString = "Unsupported audio format"; } |
| 69 | |
| 70 | if (!errorState) |
| 71 | { |
| 72 | format.rate = UnsignedInt32(in.InputNumberRaw(4)); |
| 73 | format.channels = UnsignedInt32(in.InputNumberRaw(4)); |
| 74 | |
| 75 | track.length = track.length / format.channels / (format.bits / 8); |
| 76 | } |
| 77 | |
| 78 | track.SetFormat(format); |
| 79 | |
| 80 | if (errorState) return Error(); |
| 81 | else return Success(); |
| 82 | } |
| 83 | |
| 84 | BoCA::DecoderSunAu::DecoderSunAu() |
| 85 | { |
| 86 | dataOffset = 0; |
| 87 | } |
| 88 | |
| 89 | BoCA::DecoderSunAu::~DecoderSunAu() |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | Bool BoCA::DecoderSunAu::Activate() |
| 94 | { |
| 95 | InStream in(STREAM_DRIVER, driver); |
| 96 | |
| 97 | /* Read magic number. |
| 98 | */ |
| 99 | in.InputNumber(4); |
| 100 | |
| 101 | dataOffset = in.InputNumberRaw(4); |
| 102 | |
| 103 | driver->Seek(dataOffset); |
| 104 | |
| 105 | return True; |
| 106 | } |
| 107 | |
| 108 | Bool BoCA::DecoderSunAu::Seek(Int64 samplePosition) |
| 109 | { |
| 110 | const Format &format = track.GetFormat(); |
| 111 | |
| 112 | driver->Seek(dataOffset + samplePosition * format.channels * (format.bits / 8)); |
| 113 | |
| 114 | return True; |
| 115 | } |
| 116 | |
| 117 | Int BoCA::DecoderSunAu::ReadData(Buffer<UnsignedByte> &data) |
| 118 | { |
| 119 | if (driver->GetPos() == driver->GetSize()) return -1; |
| 120 | |
| 121 | return driver->ReadData(data, data.Size()); |
| 122 | } |