Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[981634]: plugins / playback / PlayBack-aRts.cpp Maximize Restore History

Download this file

PlayBack-aRts.cpp    283 lines (239 with data), 8.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***************************************************************************
PlayBack-aRts.cpp - playback device for aRts sound daemon
-------------------
begin : Wed Jul 04 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#ifdef HAVE_ARTS_SUPPORT
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <klocale.h>
#include "PlayBack-aRts.h"
/** use at least 2^8 = 256 bytes for playback buffer */
#define MIN_PLAYBACK_BUFFER 8U
/** use at most 2^16 = 65536 bytes for playback buffer */
#define MAX_PLAYBACK_BUFFER 16U
/** Usage counter for arts_init/arts_free */
static int g_arts_usage = 0;
//***************************************************************************
PlayBackArts::PlayBackArts(QMutex &arts_lock)
:PlayBackDevice(),
m_stream(0),
m_rate(0),
m_channels(0),
m_bits(0),
m_buffer(),
m_buffer_size(0),
m_buffer_used(0),
m_lock_aRts(arts_lock),
m_closed(true)
{
qDebug("PlayBackArts::PlayBackArts(...)");
}
//***************************************************************************
PlayBackArts::~PlayBackArts()
{
}
//***************************************************************************
QString PlayBackArts::open(const QString &, double rate,
unsigned int channels, unsigned int bits,
unsigned int bufbase)
{
qDebug("PlayBackArts::open(rate=%f,channels=%u,"\
"bits=%u, bufbase=%u)", rate, channels,
bits, bufbase);
if ((channels < 1) || (rate < 1))
return i18n("invalid playback parameters");
m_rate = rate;
m_channels = channels;
m_bits = bits;
m_buffer_size = 0;
m_buffer_used = 0;
m_stream = 0;
QMutexLocker lock(&m_lock_aRts);
Q_ASSERT(m_closed);
m_closed = false;
// initialize aRts playback
if (!g_arts_usage) {
int errorcode = arts_init();
if (errorcode < 0) {
qWarning("PlayBackArts::open(): arts_init error: %s",
arts_error_text(errorcode));
return artsErrorText(errorcode);
}
}
g_arts_usage++;
// open the stream
m_stream = arts_play_stream((int)m_rate, m_bits, m_channels, "Kwave");
Q_ASSERT(m_stream);
if (!m_stream) return i18n("unable to open aRts playback stream");
m_closed = false;
// calculate the buffer size and limit it if necessary
// ask aRts for the optimum and ignore the user's setting
m_buffer_size = arts_stream_get(m_stream, ARTS_P_PACKET_SIZE);
Q_ASSERT(m_buffer_size >= (1U << MIN_PLAYBACK_BUFFER));
Q_ASSERT(m_buffer_size <= (1U << MAX_PLAYBACK_BUFFER));
if (m_buffer_size < (1U << MIN_PLAYBACK_BUFFER))
m_buffer_size = (1U << MIN_PLAYBACK_BUFFER);
if (m_buffer_size > (1U << MAX_PLAYBACK_BUFFER))
m_buffer_size = (1U << MAX_PLAYBACK_BUFFER);
qDebug("PlayBackArts::open(): using %u bytes as buffer", m_buffer_size);
m_buffer.resize(m_buffer_size);
m_buffer.fill(0);
return 0;
}
//***************************************************************************
int PlayBackArts::write(QMemArray<sample_t> &samples)
{
Q_ASSERT (m_buffer_used < m_buffer_size);
if (m_buffer_used >= m_buffer_size) {
qWarning("PlayBackArts::write(): buffer overflow ?!");
return -EIO;
}
Q_ASSERT(samples.count() == m_channels);
if (samples.count() != m_channels) {
qDebug("PlayBackArts::write samples.count()=%d but m_channels=%d",
samples.count(), m_channels);
return -EINVAL;
}
// convert into byte stream
unsigned int channel;
for (channel=0; channel < m_channels; channel++) {
sample_t sample = samples[channel];
switch (m_bits) {
case 8:
sample += 1 << 23;
m_buffer[m_buffer_used++] = sample >> 16;
break;
case 16:
sample += 1 << 23;
m_buffer[m_buffer_used++] = sample >> 8;
m_buffer[m_buffer_used++] = (sample >> 16) + 128;
break;
case 24:
// play in 32 bit format
m_buffer[m_buffer_used++] = 0x00;
m_buffer[m_buffer_used++] = sample & 0x0FF;
m_buffer[m_buffer_used++] = sample >> 8;
m_buffer[m_buffer_used++] = (sample >> 16) + 128;
break;
default:
return -EINVAL;
}
}
// write buffer to device if full
if (m_buffer_used >= m_buffer_size) flush();
return 0;
}
//***************************************************************************
void PlayBackArts::flush()
{
if (!m_buffer_used) return; // nothing to do
Q_ASSERT(m_buffer_used <= m_buffer_size);
Q_ASSERT(m_stream);
if (m_stream) {
QMutexLocker lock(&m_lock_aRts);
// fill rest of buffer with zeroes
while (m_buffer_used < m_buffer_size)
m_buffer[m_buffer_used++] = 0;
int errorcode = arts_write(m_stream, m_buffer.data(), m_buffer_used);
if (errorcode < 0) {
qWarning("PlayBackArts: arts_write error: %s",
arts_error_text(errorcode));
}
}
m_buffer_used = 0;
}
//***************************************************************************
int PlayBackArts::close()
{
qDebug("PlayBackArts::close() [pid %d]",(int)pthread_self());
flush();
{
QMutexLocker lock(&m_lock_aRts);
// close the playback stream
if (m_stream) arts_close_stream(m_stream);
if (!m_closed && g_arts_usage && !--g_arts_usage) {
qDebug("PlayBackArts::close() [pid %d]: releasing aRts API",
(int)pthread_self());
arts_free();
}
m_stream = 0;
m_closed = true;
}
return 0;
}
//***************************************************************************
QString PlayBackArts::artsErrorText(int errorcode)
{
QString text = "?";
switch (errorcode) {
case ARTS_E_NOSERVER:
return(i18n("Can't connect to aRts soundserver. "\
"Maybe it is not properly installed or not running."));
break;
case ARTS_E_NOBACKEND:
return(i18n("Loading the aRts backend library failed. "\
"This might be due to a problem with your aRts "\
"installation."));
break;
case ARTS_E_NOSTREAM:
return(i18n("The aRts output stream is invalid."));
break;
case ARTS_E_NOINIT:
return(i18n("aRts output is not available because the "\
"initialization of the aRts output has failed "\
"or not been done."));
break;
case ARTS_E_NOIMPL:
return(i18n("The required aRts function is not yet "\
"implemented. Maybe you should get a newer version of "
"aRts or upgrade the kdelibs package."));
break;
default:
// unknown error: fall-back to aRts built-in error message
text = i18n(arts_error_text(errorcode));
};
return text;
}
//***************************************************************************
QStringList PlayBackArts::supportedDevices()
{
QStringList list; // list stays empty, no selectable devices
list.append(i18n("[aRts sound daemon]"));
return list;
}
//***************************************************************************
QValueList<unsigned int> PlayBackArts::supportedBits(const QString &device)
{
(void)device;
QValueList <unsigned int> bits;
// aRts supports only 8 and 16 bits as far as I know...
bits.append(8);
bits.append(16);
return bits;
}
//***************************************************************************
int PlayBackArts::detectChannels(const QString &device,
unsigned int &min, unsigned int &max)
{
(void) device;
// aRts supports only mono and stereo as far as I know...
min = 1;
max = 2;
return 0;
}
#endif /* HAVE_ARTS_SUPPORT */
//***************************************************************************
//***************************************************************************