| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-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 "player.h" |
| 14 | |
| 15 | using namespace BoCA::AS; |
| 16 | |
| 17 | using namespace smooth::IO; |
| 18 | |
| 19 | BoCA::Player::Player() |
| 20 | { |
| 21 | output = NIL; |
| 22 | |
| 23 | playing = False; |
| 24 | stop = False; |
| 25 | } |
| 26 | |
| 27 | BoCA::Player::~Player() |
| 28 | { |
| 29 | Stop(); |
| 30 | } |
| 31 | |
| 32 | Void BoCA::Player::Play(const String &fileName) |
| 33 | { |
| 34 | /* Stop playback if already playing. |
| 35 | */ |
| 36 | if (playing) Stop(); |
| 37 | |
| 38 | /* Play selected track. |
| 39 | */ |
| 40 | playing = True; |
| 41 | stop = False; |
| 42 | |
| 43 | NonBlocking1<String>(&Player::PlayThread, this).Call(fileName); |
| 44 | } |
| 45 | |
| 46 | Void BoCA::Player::Stop() |
| 47 | { |
| 48 | if (!playing || stop) return; |
| 49 | |
| 50 | /* Request stop of playback. |
| 51 | */ |
| 52 | stop = True; |
| 53 | |
| 54 | while (playing) S::System::System::Sleep(10); |
| 55 | } |
| 56 | |
| 57 | Int BoCA::Player::PlayThread(String fileName) |
| 58 | { |
| 59 | Registry &boca = Registry::Get(); |
| 60 | |
| 61 | /* Create decoder. |
| 62 | */ |
| 63 | DecoderComponent *decoder = boca.CreateDecoderForStream(fileName); |
| 64 | |
| 65 | if (decoder == NIL) |
| 66 | { |
| 67 | playing = False; |
| 68 | |
| 69 | return Error(); |
| 70 | } |
| 71 | |
| 72 | /* Get track info. |
| 73 | */ |
| 74 | Track track; |
| 75 | Error error = decoder->GetStreamInfo(fileName, track); |
| 76 | String errorString = decoder->GetErrorString(); |
| 77 | |
| 78 | if (error != Success()) |
| 79 | { |
| 80 | boca.DeleteComponent(decoder); |
| 81 | |
| 82 | playing = False; |
| 83 | |
| 84 | return Error(); |
| 85 | } |
| 86 | |
| 87 | /* Add decoder to stream. |
| 88 | */ |
| 89 | InStream *stream = new InStream(STREAM_FILE, fileName, IS_READ); |
| 90 | |
| 91 | decoder->SetConfiguration(Config::Get()); |
| 92 | decoder->SetAudioTrackInfo(track); |
| 93 | |
| 94 | if (stream->SetFilter(decoder) == False) |
| 95 | { |
| 96 | delete stream; |
| 97 | |
| 98 | boca.DeleteComponent(decoder); |
| 99 | |
| 100 | playing = False; |
| 101 | |
| 102 | return Error(); |
| 103 | } |
| 104 | |
| 105 | /* Create output component. |
| 106 | */ |
| 107 | for (Int i = 0; i < boca.GetNumberOfComponents(); i++) |
| 108 | { |
| 109 | if (boca.GetComponentType(i) != BoCA::COMPONENT_TYPE_OUTPUT) continue; |
| 110 | |
| 111 | output = (OutputComponent *) boca.CreateComponentByID(boca.GetComponentID(i)); |
| 112 | |
| 113 | if (output == NIL) continue; |
| 114 | |
| 115 | output->SetConfiguration(Config::Get()); |
| 116 | output->SetAudioTrackInfo(track); |
| 117 | |
| 118 | if (output->Activate()) |
| 119 | { |
| 120 | /* Enter playback loop. |
| 121 | */ |
| 122 | if (!output->GetErrorState()) Loop(track, stream); |
| 123 | |
| 124 | /* Clean up. |
| 125 | */ |
| 126 | output->Deactivate(); |
| 127 | |
| 128 | error = Success(); |
| 129 | } |
| 130 | |
| 131 | boca.DeleteComponent(output); |
| 132 | |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | delete stream; |
| 137 | |
| 138 | boca.DeleteComponent(decoder); |
| 139 | |
| 140 | playing = false; |
| 141 | |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | Void BoCA::Player::Loop(const Track &track, InStream *stream) |
| 146 | { |
| 147 | /* Enter playback loop. |
| 148 | */ |
| 149 | const Format &format = track.GetFormat(); |
| 150 | |
| 151 | Int64 position = 0; |
| 152 | UnsignedLong samplesSize = format.rate / 4; |
| 153 | |
| 154 | Int bytesPerSample = format.bits / 8 * format.channels; |
| 155 | Buffer<UnsignedByte> buffer(samplesSize * bytesPerSample); |
| 156 | |
| 157 | while (!stop) |
| 158 | { |
| 159 | /* Find step size. |
| 160 | */ |
| 161 | Int step = samplesSize; |
| 162 | |
| 163 | if (track.length >= 0) |
| 164 | { |
| 165 | if (position >= track.length) break; |
| 166 | |
| 167 | if (position + step > track.length) step = track.length - position; |
| 168 | } |
| 169 | |
| 170 | buffer.Resize(step * bytesPerSample); |
| 171 | |
| 172 | /* Read samples from stream. |
| 173 | */ |
| 174 | Int bytes = stream->InputData(buffer, buffer.Size()); |
| 175 | |
| 176 | if (bytes == 0) break; |
| 177 | |
| 178 | buffer.Resize(bytes); |
| 179 | |
| 180 | /* Write data. |
| 181 | */ |
| 182 | Write(buffer, step * bytesPerSample); |
| 183 | } |
| 184 | |
| 185 | if (!stop) output->Finish(); |
| 186 | |
| 187 | while (!stop && output->IsPlaying()) S::System::System::Sleep(20); |
| 188 | |
| 189 | stop = True; |
| 190 | } |
| 191 | |
| 192 | Void BoCA::Player::Write(Buffer<UnsignedByte> &buffer, Int chunkSize) |
| 193 | { |
| 194 | while (buffer.Size() > 0) |
| 195 | { |
| 196 | while (output->CanWrite() < chunkSize && !stop) S::System::System::Sleep(10); |
| 197 | |
| 198 | if (stop) break; |
| 199 | |
| 200 | Buffer<UnsignedByte> outputBuffer(Math::Min(buffer.Size(), output->CanWrite())); |
| 201 | |
| 202 | memcpy(outputBuffer, buffer, outputBuffer.Size()); |
| 203 | memmove(buffer, buffer + outputBuffer.Size(), buffer.Size() - outputBuffer.Size()); |
| 204 | |
| 205 | buffer.Resize(buffer.Size() - outputBuffer.Size()); |
| 206 | |
| 207 | output->WriteData(outputBuffer); |
| 208 | } |
| 209 | } |