| 1 |
// Copyright 2004 "Gilles Degottex" |
| 2 |
|
| 3 |
// This file is part of "Music" |
| 4 |
|
| 5 |
// "Music" is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation; either version 2.1 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// "Music" is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with this program; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
| 19 |
|
| 20 |
#include "CaptureThread.h" |
| 21 |
|
| 22 |
#include <cassert> |
| 23 |
#include <time.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <iostream> |
| 27 |
#include <fstream> |
| 28 |
#include <vector> |
| 29 |
using namespace std; |
| 30 |
#include <qdatetime.h> |
| 31 |
|
| 32 |
double DecodeUnsigned8Bits(void* buffer, int i) {return 2*(((unsigned char*)buffer)[i])/256.0 - 1;} |
| 33 |
double DecodeSigned8Bits(void* buffer, int i) {return (((signed char*)buffer)[i])/128.0;} |
| 34 |
double DecodeUnsigned16Bits(void* buffer, int i){return 2*((unsigned short*)buffer)[i]/65536.0 - 1;} |
| 35 |
double DecodeSigned16Bits(void* buffer, int i) {return ((signed short*)buffer)[i]/32768.0;} |
| 36 |
double DecodeUnsigned24BitsIn3BytesLE(void* buffer, int i) { |
| 37 |
quint32 lsb = ((quint8*)buffer)[3*i]; |
| 38 |
quint32 middle1 = ((quint8*)buffer)[3*i+1]; |
| 39 |
quint32 msb = ((quint8*)buffer)[3*i+2]; |
| 40 |
quint32 combined = (msb << 16) + (middle1 << 8) + lsb; |
| 41 |
return 2*(combined/16777216.0) - 1; |
| 42 |
} |
| 43 |
double DecodeSigned24BitsIn3BytesLE(void* buffer, int i) { |
| 44 |
qint32 combined; |
| 45 |
|
| 46 |
combined = ((quint32) ((quint8*)buffer)[3*i+2]) << 24; |
| 47 |
combined += ((quint8*)buffer)[3*i+1] << 16 ; |
| 48 |
combined += ((quint8*)buffer)[3*i] << 8 ; |
| 49 |
|
| 50 |
return combined/2147483648.0; |
| 51 |
} |
| 52 |
|
| 53 |
void AddValue2ChannelFirst(CaptureThreadImpl* impl, double value, int i) |
| 54 |
{ |
| 55 |
if(i%2==0) |
| 56 |
impl->m_capture_thread->m_values.push_front(value); |
| 57 |
} |
| 58 |
void AddValue2ChannelMix(CaptureThreadImpl* impl, double value, int i) |
| 59 |
{ |
| 60 |
if(i%2==0) |
| 61 |
impl->m_tmp_value = value; |
| 62 |
// impl->m_capture_thread->m_values.push_front(value); |
| 63 |
else |
| 64 |
impl->m_capture_thread->m_values.push_front((impl->m_tmp_value+value)/2.0); |
| 65 |
// impl->m_capture_thread->m_values[0] = (impl->m_capture_thread->m_values[0]+value)/2.0; |
| 66 |
} |
| 67 |
void AddValue1Channel(CaptureThreadImpl* impl, double value, int i) |
| 68 |
{ |
| 69 |
Q_UNUSED(i) |
| 70 |
|
| 71 |
impl->m_capture_thread->m_values.push_front(value); |
| 72 |
} |
| 73 |
|
| 74 |
CaptureThread::CaptureThread(const QString& name) |
| 75 |
{ |
| 76 |
m_current_impl = NULL; |
| 77 |
|
| 78 |
m_capturing = false; |
| 79 |
m_packet_size = 0; |
| 80 |
m_ext_lock = false; |
| 81 |
m_packet_size_sll = 0; |
| 82 |
|
| 83 |
m_pause = false; |
| 84 |
m_mix_multiple_channel = false; |
| 85 |
|
| 86 |
m_name = name; |
| 87 |
|
| 88 |
#ifdef CAPTURE_QT |
| 89 |
m_impls.push_back(new CaptureThreadImplQt(this)); |
| 90 |
#endif |
| 91 |
#ifdef CAPTURE_JACK |
| 92 |
m_impls.push_back(new CaptureThreadImplJACK(this)); |
| 93 |
#endif |
| 94 |
#ifdef CAPTURE_ALSA |
| 95 |
m_impls.push_back(new CaptureThreadImplALSA(this)); |
| 96 |
#endif |
| 97 |
#ifdef CAPTURE_OSS |
| 98 |
m_impls.push_back(new CaptureThreadImplOSS(this)); |
| 99 |
#endif |
| 100 |
#ifdef CAPTURE_PORTAUDIO |
| 101 |
m_impls.push_back(new CaptureThreadImplPortAudio(this)); |
| 102 |
#endif |
| 103 |
|
| 104 |
listTransports(); |
| 105 |
} |
| 106 |
|
| 107 |
void CaptureThread::autoDetectTransport() |
| 108 |
{ |
| 109 |
bool was_capturing = isCapturing(); |
| 110 |
if(was_capturing) |
| 111 |
stopCapture(); |
| 112 |
|
| 113 |
QString old_name; |
| 114 |
if(m_current_impl!=NULL) |
| 115 |
old_name = m_current_impl->m_name; |
| 116 |
|
| 117 |
cout << "CaptureThread: INFO: Auto detecting a working transport ... " << flush; |
| 118 |
|
| 119 |
CaptureThreadImpl* impl = NULL; |
| 120 |
for(vector<CaptureThreadImpl*>::iterator it=m_impls.begin(); impl==NULL && it!=m_impls.end(); it++) |
| 121 |
if((*it)->is_available()) |
| 122 |
impl = *it; |
| 123 |
|
| 124 |
if(impl!=NULL) |
| 125 |
{ |
| 126 |
m_current_impl = impl; |
| 127 |
|
| 128 |
cout << "using " << m_current_impl->m_name.toStdString() << endl; |
| 129 |
|
| 130 |
if(m_current_impl->m_name!=old_name) |
| 131 |
emit(transportChanged(m_current_impl->m_name)); |
| 132 |
|
| 133 |
if(was_capturing) |
| 134 |
startCapture(); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
cout << "no working transport !" << endl; |
| 139 |
|
| 140 |
if(old_name!="") |
| 141 |
emit(transportChanged("")); |
| 142 |
} |
| 143 |
} |
| 144 |
void CaptureThread::selectTransport(const QString& name) |
| 145 |
{ |
| 146 |
cout << "CaptureThread: INFO: using " << name.toStdString() << " transport" << endl; |
| 147 |
if(getCurrentTransport() && name==getCurrentTransport()->getName()) return; |
| 148 |
|
| 149 |
bool was_capturing = isCapturing(); |
| 150 |
if(was_capturing) |
| 151 |
stopCapture(); |
| 152 |
|
| 153 |
QString old_name; |
| 154 |
if(m_current_impl!=NULL) |
| 155 |
old_name = m_current_impl->m_name; |
| 156 |
|
| 157 |
CaptureThreadImpl* impl = NULL; |
| 158 |
for(vector<CaptureThreadImpl*>::iterator it=m_impls.begin(); impl==NULL && it!=m_impls.end(); it++) |
| 159 |
if((*it)->m_name==name) |
| 160 |
impl = *it; |
| 161 |
|
| 162 |
if(impl==NULL) |
| 163 |
{ |
| 164 |
cout << "CaptureThread: ERROR: unknown transport '" << name.toStdString() << "'" << endl; |
| 165 |
throw QString("CaptureThread: ERROR: unknown transport '")+name+"'"; |
| 166 |
} |
| 167 |
|
| 168 |
m_current_impl = impl; |
| 169 |
|
| 170 |
if(m_current_impl->m_name!=old_name) |
| 171 |
emit(transportChanged(m_current_impl->m_name)); |
| 172 |
|
| 173 |
if(was_capturing) |
| 174 |
startCapture(); |
| 175 |
} |
| 176 |
void CaptureThread::selectTransport(int index) |
| 177 |
{ |
| 178 |
assert(index>=0 && index<int(m_impls.size())); |
| 179 |
|
| 180 |
if(m_impls[index]==getCurrentTransport()) return; |
| 181 |
cout << "CaptureThread: INFO: change transport to " << m_impls[index]->getName().toStdString() << " transport" << endl; |
| 182 |
|
| 183 |
bool was_capturing = isCapturing(); |
| 184 |
if(was_capturing) |
| 185 |
stopCapture(); |
| 186 |
|
| 187 |
m_current_impl = m_impls[index]; |
| 188 |
|
| 189 |
emit(transportChanged(m_current_impl->m_name)); |
| 190 |
|
| 191 |
if(was_capturing) |
| 192 |
startCapture(); |
| 193 |
} |
| 194 |
const vector<CaptureThreadImpl*>& CaptureThread::getTransports() const |
| 195 |
{ |
| 196 |
return m_impls; |
| 197 |
} |
| 198 |
void CaptureThread::listTransports() |
| 199 |
{ |
| 200 |
cout << "CaptureThread: INFO: Built-in transports" << endl; |
| 201 |
for(vector<CaptureThreadImpl*>::iterator it=m_impls.begin(); it!=m_impls.end(); it++) |
| 202 |
cout << "CaptureThread: INFO: " << (*it)->getStatus().toStdString() << " " << (*it)->m_name.toStdString() << " " << (*it)->m_descr.toStdString() << endl; |
| 203 |
} |
| 204 |
const CaptureThreadImpl* CaptureThread::getCurrentTransport() const |
| 205 |
{ |
| 206 |
return m_current_impl; |
| 207 |
} |
| 208 |
int CaptureThread::getCurrentTransportIndex() const |
| 209 |
{ |
| 210 |
for(int i=0; i<int(m_impls.size()); i++) |
| 211 |
if(m_impls[i]==getCurrentTransport()) |
| 212 |
return i; |
| 213 |
|
| 214 |
return -1; |
| 215 |
} |
| 216 |
QString CaptureThread::getCurrentTransportDescr() const |
| 217 |
{ |
| 218 |
if(m_current_impl==NULL) |
| 219 |
return ""; |
| 220 |
|
| 221 |
return m_current_impl->m_descr; |
| 222 |
} |
| 223 |
const CaptureThreadImpl* CaptureThread::getTransport(const QString& name) const |
| 224 |
{ |
| 225 |
for(vector<CaptureThreadImpl*>::const_iterator it=m_impls.begin(); it!=m_impls.end(); it++) |
| 226 |
if((*it)->m_name==name) |
| 227 |
return *it; |
| 228 |
|
| 229 |
return NULL; |
| 230 |
} |
| 231 |
QString CaptureThread::getFormatDescr() const |
| 232 |
{ |
| 233 |
if(m_current_impl==NULL) |
| 234 |
return ""; |
| 235 |
|
| 236 |
return ""; // TODO |
| 237 |
} |
| 238 |
|
| 239 |
void CaptureThread::emitError(const QString& error) |
| 240 |
{ |
| 241 |
emit(errorRaised(error)); |
| 242 |
} |
| 243 |
void CaptureThread::emitSamplingRateChanged() |
| 244 |
{ |
| 245 |
if(m_current_impl->m_sampling_rate>0) |
| 246 |
emit(samplingRateChanged(m_current_impl->m_sampling_rate)); |
| 247 |
} |
| 248 |
void CaptureThread::emitCaptureStarted() |
| 249 |
{ |
| 250 |
emit(captureStarted()); |
| 251 |
} |
| 252 |
void CaptureThread::emitCaptureStoped() |
| 253 |
{ |
| 254 |
emit(captureStoped()); |
| 255 |
} |
| 256 |
void CaptureThread::emitCaptureToggled(bool value) |
| 257 |
{ |
| 258 |
emit(captureToggled(value)); |
| 259 |
} |
| 260 |
|
| 261 |
void CaptureThread::startCapture() |
| 262 |
{ |
| 263 |
if(m_current_impl==NULL) return; |
| 264 |
|
| 265 |
m_current_impl->startCapture(); |
| 266 |
} |
| 267 |
void CaptureThread::stopCapture() |
| 268 |
{ |
| 269 |
// cout << "CaptureThread::stopCapture" << endl; |
| 270 |
|
| 271 |
if(m_current_impl==NULL) return; |
| 272 |
|
| 273 |
m_current_impl->stopCapture(); |
| 274 |
|
| 275 |
// cout << "/CaptureThread::stopCapture" << endl; |
| 276 |
} |
| 277 |
|
| 278 |
void CaptureThread::toggleCapture(bool run) |
| 279 |
{ |
| 280 |
if(run && !m_capturing) startCapture(); |
| 281 |
if(!run && m_capturing) stopCapture(); |
| 282 |
} |
| 283 |
|
| 284 |
void CaptureThread::reset() |
| 285 |
{ |
| 286 |
stopCapture(); |
| 287 |
startCapture(); |
| 288 |
} |
| 289 |
|
| 290 |
void CaptureThread::togglePause(bool pause) |
| 291 |
{ |
| 292 |
m_pause = pause; |
| 293 |
} |
| 294 |
|
| 295 |
int CaptureThread::getSamplingRate() const |
| 296 |
{ |
| 297 |
if(m_current_impl==NULL) return SAMPLING_RATE_UNKNOWN; |
| 298 |
|
| 299 |
return m_current_impl->m_sampling_rate; |
| 300 |
} |
| 301 |
void CaptureThread::setSamplingRate(int rate) |
| 302 |
{ |
| 303 |
if(m_current_impl!=NULL) |
| 304 |
m_current_impl->setSamplingRate(rate); |
| 305 |
} |
| 306 |
void CaptureThread::setSource(const QString& name) |
| 307 |
{ |
| 308 |
if(m_current_impl==NULL) |
| 309 |
{ |
| 310 |
cout << "CaptureThread: setSource: ERROR: select a transport first" << endl; |
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
if(name!=m_current_impl->m_source) |
| 315 |
{ |
| 316 |
m_current_impl->m_source = name; |
| 317 |
if(isCapturing()) |
| 318 |
{ |
| 319 |
stopCapture(); |
| 320 |
startCapture(); |
| 321 |
} |
| 322 |
|
| 323 |
emit(sourceChanged(m_current_impl->m_source)); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void CaptureThread::setMixMultipleChannels(bool mix) |
| 328 |
{ |
| 329 |
m_mix_multiple_channel = mix; |
| 330 |
} |
| 331 |
|
| 332 |
CaptureThread::~CaptureThread() |
| 333 |
{ |
| 334 |
stopCapture(); |
| 335 |
|
| 336 |
for(vector<CaptureThreadImpl*>::iterator it=m_impls.begin(); it!=m_impls.end(); it++) |
| 337 |
delete *it; |
| 338 |
} |
| 339 |
|
| 340 |
// -------------------------------- implementation ------------------------------ |
| 341 |
|
| 342 |
CaptureThreadImpl::CaptureThreadImpl(CaptureThread* capture_thread, const QString& name, const QString& descr) |
| 343 |
: m_capture_thread(capture_thread) |
| 344 |
{ |
| 345 |
m_name = name; |
| 346 |
m_descr = descr; |
| 347 |
m_status = ""; |
| 348 |
|
| 349 |
m_sampling_rate = CaptureThread::SAMPLING_RATE_UNKNOWN; |
| 350 |
m_format_size = 0; |
| 351 |
m_format_signed = true; |
| 352 |
m_channel_count = 0; |
| 353 |
m_source = ""; |
| 354 |
} |
| 355 |
|
| 356 |
const QString& CaptureThreadImpl::getStatus() |
| 357 |
{ |
| 358 |
if(m_status=="") |
| 359 |
is_available(); |
| 360 |
|
| 361 |
return m_status; |
| 362 |
} |
| 363 |
|
| 364 |
QString CaptureThreadImpl::getASCIISource() const |
| 365 |
{ |
| 366 |
QString untranssource = m_source; |
| 367 |
|
| 368 |
if(untranssource==QObject::tr("default")) |
| 369 |
untranssource = "default"; |
| 370 |
|
| 371 |
return untranssource.toLatin1(); |
| 372 |
} |
| 373 |
|
| 374 |
void CaptureThreadImpl::setFormatDescrsAndFns(int format_size, bool format_signed, bool format_float, int channel_count) |
| 375 |
{ |
| 376 |
m_format_size = format_size; |
| 377 |
m_format_signed = format_signed; |
| 378 |
m_format_float = format_float; |
| 379 |
m_channel_count = channel_count; |
| 380 |
|
| 381 |
if(m_format_size==3) // 24bits |
| 382 |
{ |
| 383 |
if(m_format_signed) decodeValue = DecodeSigned24BitsIn3BytesLE; |
| 384 |
else decodeValue = DecodeUnsigned24BitsIn3BytesLE; |
| 385 |
} |
| 386 |
if(m_format_size==2) // 16bits |
| 387 |
{ |
| 388 |
if(m_format_signed) decodeValue = DecodeSigned16Bits; |
| 389 |
else decodeValue = DecodeUnsigned16Bits; |
| 390 |
} |
| 391 |
else // 8bits |
| 392 |
{ |
| 393 |
if(m_format_signed) decodeValue = DecodeSigned8Bits; |
| 394 |
else decodeValue = DecodeUnsigned8Bits; |
| 395 |
} |
| 396 |
|
| 397 |
if(m_channel_count==1) |
| 398 |
addValue = AddValue1Channel; |
| 399 |
else if(m_channel_count==2) |
| 400 |
{ |
| 401 |
if(m_capture_thread->m_mix_multiple_channel) |
| 402 |
addValue = AddValue2ChannelMix; |
| 403 |
else |
| 404 |
addValue = AddValue2ChannelFirst; |
| 405 |
} |
| 406 |
|
| 407 |
cout << "CaptureThread: INFO: format is " << (m_format_signed?"signed":"unsigned") << " " << (m_format_float?"float":"integer") << " " << m_format_size*8 << "bits with " << m_channel_count << " channel(s)" << endl; |
| 408 |
} |