Debian Codegraph @

components/output/haiku/haiku.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2017 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 <SoundPlayer.h>
14
15 #include "haiku.h"
16
17 using namespace smooth::Threads;
18
19 const String &BoCA::OutputHaiku::GetComponentSpecs()
20 {
21 static String componentSpecs = " \
22 \
23 <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
24 <component> \
25 <name>Haiku Output Plugin</name> \
26 <version>1.0</version> \
27 <id>haiku-out</id> \
28 <type>output</type> \
29 <input bits=\"8,16,32\"/> \
30 <input float=\"true\"/> \
31 </component> \
32 \
33 ";
34
35 return componentSpecs;
36 }
37
38 namespace BoCA
39 {
40 void PlayBuffer(void *, void *, size_t, const media_raw_audio_format &);
41 };
42
43 BoCA::OutputHaiku::OutputHaiku()
44 {
45 player = NIL;
46 paused = False;
47
48 samplesBufferMutex = new Mutex();
49 }
50
51 BoCA::OutputHaiku::~OutputHaiku()
52 {
53 delete samplesBufferMutex;
54 }
55
56 Bool BoCA::OutputHaiku::Activate()
57 {
58 static Endianness endianness = CPU().GetEndianness();
59
60 /* Define stream format.
61 */
62 const Format &format = track.GetFormat();
63 media_raw_audio_format fmt = media_raw_audio_format::wildcard;
64
65 fmt.buffer_size = 2048 * format.channels * (format.bits / 8);
66 fmt.byte_order = (endianness == EndianBig ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN);
67 fmt.frame_rate = format.rate;
68 fmt.channel_count = format.channels;
69
70 if (format.bits == 8 ) fmt.format = media_raw_audio_format::B_AUDIO_CHAR;
71 else if (format.bits == 16 ) fmt.format = media_raw_audio_format::B_AUDIO_SHORT;
72 else if (format.bits == 32 && !format.fp) fmt.format = media_raw_audio_format::B_AUDIO_INT;
73 else if (format.bits == 32 && format.fp) fmt.format = media_raw_audio_format::B_AUDIO_FLOAT;
74
75 /* Preallocate sample buffer.
76 */
77 samplesBuffer.Resize((format.rate / 4 + 2048) * format.channels * (format.bits / 8));
78 samplesBuffer.Resize(0);
79
80 /* Start sound player.
81 */
82 player = new BSoundPlayer(&fmt, "Haiku Output Plugin", PlayBuffer, NULL, this);
83
84 player->SetHasData(true);
85 player->Start();
86
87 paused = False;
88
89 return True;
90 }
91
92 Bool BoCA::OutputHaiku::Deactivate()
93 {
94 /* Stop sound player.
95 */
96 player->Stop();
97
98 delete player;
99
100 player = NIL;
101
102 return True;
103 }
104
105 Int BoCA::OutputHaiku::WriteData(Buffer<UnsignedByte> &data)
106 {
107 samplesBufferMutex->Lock();
108
109 Int bytes = samplesBuffer.Size();
110
111 samplesBuffer.Resize(bytes + data.Size());
112
113 memcpy(samplesBuffer + bytes, data, data.Size());
114
115 samplesBufferMutex->Release();
116
117 return data.Size();
118 }
119
120 Int BoCA::OutputHaiku::CanWrite()
121 {
122 if (player == NIL || paused) return 0;
123
124 const Format &format = track.GetFormat();
125
126 samplesBufferMutex->Lock();
127
128 Int canWrite = (format.rate / 4 + 2048) * format.channels * (format.bits / 8) - samplesBuffer.Size();
129
130 samplesBufferMutex->Release();
131
132 return canWrite;
133 }
134
135 Int BoCA::OutputHaiku::SetPause(Bool pause)
136 {
137 if (player == NIL) return Error();
138
139 if (pause) player->Stop(false, false);
140 else player->Start();
141
142 paused = pause;
143
144 return Success();
145 }
146
147 Bool BoCA::OutputHaiku::IsPlaying()
148 {
149 if (player == NIL) return False;
150
151 samplesBufferMutex->Lock();
152
153 Bool isPlaying = (samplesBuffer.Size() > 0);
154
155 samplesBufferMutex->Release();
156
157 return isPlaying;
158 }
159
160 void BoCA::PlayBuffer(void *cookie, void *buffer, size_t size, const media_raw_audio_format &format)
161 {
162 OutputHaiku *component = (OutputHaiku *) cookie;
163
164 component->samplesBufferMutex->Lock();
165
166 UnsignedInt numBytes = Math::Min(component->samplesBuffer.Size(), size);
167
168 /* Copy samples to output buffer.
169 */
170 if (numBytes < size) memset(buffer, 0, size);
171
172 memcpy(buffer, component->samplesBuffer, numBytes);
173
174 /* Move remaining samples to the start of the buffer.
175 */
176 memmove(component->samplesBuffer, component->samplesBuffer + numBytes, component->samplesBuffer.Size() - numBytes);
177
178 component->samplesBuffer.Resize(component->samplesBuffer.Size() - numBytes);
179
180 component->samplesBufferMutex->Release();
181 }