| 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 <unistd.h> |
| 27 |
#include <iostream> |
| 28 |
#include <fstream> |
| 29 |
#include <vector> |
| 30 |
using namespace std; |
| 31 |
|
| 32 |
#include <QList> |
| 33 |
#include <QSet> |
| 34 |
#include <QCoreApplication> |
| 35 |
|
| 36 |
// ------------------------------ Qt implementation ---------------------------- |
| 37 |
#ifdef CAPTURE_QT |
| 38 |
|
| 39 |
CaptureThreadImplQt::CaptureThreadImplQt(CaptureThread* capture_thread) |
| 40 |
: CaptureThreadImpl(capture_thread, "Qt", QString("Qt (lib:")+qVersion()+")") |
| 41 |
, m_audioInputDevice(QAudioDeviceInfo::defaultInputDevice()) |
| 42 |
, m_audioInput(NULL) |
| 43 |
, m_audioInputIODevice(NULL) |
| 44 |
{ |
| 45 |
m_source = "default"; |
| 46 |
} |
| 47 |
|
| 48 |
bool CaptureThreadImplQt::is_available() |
| 49 |
{ |
| 50 |
try |
| 51 |
{ |
| 52 |
if(QAudioDeviceInfo::availableDevices(QAudio::AudioInput).count() == 0) |
| 53 |
throw QString("Qt: is_available: no device available"); |
| 54 |
} |
| 55 |
catch(QString error) |
| 56 |
{ |
| 57 |
m_status = "N/A"; |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
m_status = "OK"; |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
void CaptureThreadImplQt::capture_init() |
| 67 |
{ |
| 68 |
// cout << "CaptureThreadImplQt::capture_init" << endl; |
| 69 |
|
| 70 |
set_params(false); |
| 71 |
|
| 72 |
if (m_audioInput) { |
| 73 |
if (QAudio::SuspendedState == m_state) |
| 74 |
m_audioInput->stop(); |
| 75 |
|
| 76 |
m_audioInputIODevice = m_audioInput->start(); |
| 77 |
connect(m_audioInputIODevice, SIGNAL(readyRead()), |
| 78 |
this, SLOT(audioDataReady())); |
| 79 |
} |
| 80 |
|
| 81 |
m_capture_thread->m_capturing = true; |
| 82 |
m_capture_thread->emitCaptureStarted(); |
| 83 |
m_capture_thread->emitCaptureToggled(true); |
| 84 |
|
| 85 |
// cout << "CaptureThreadImplQt::~capture_init" << endl; |
| 86 |
} |
| 87 |
|
| 88 |
void CaptureThreadImplQt::capture_finished() |
| 89 |
{ |
| 90 |
if (m_audioInput) { |
| 91 |
m_audioInput->stop(); |
| 92 |
QCoreApplication::instance()->processEvents(); |
| 93 |
m_audioInput->disconnect(); |
| 94 |
} |
| 95 |
m_audioInputIODevice = NULL; |
| 96 |
|
| 97 |
m_capture_thread->m_capturing = false; |
| 98 |
m_capture_thread->emitCaptureStoped(); |
| 99 |
m_capture_thread->emitCaptureToggled(false); |
| 100 |
} |
| 101 |
|
| 102 |
void CaptureThreadImplQt::startCapture() |
| 103 |
{ |
| 104 |
// cout << "CaptureThreadImplQt::startCapture" << endl; |
| 105 |
|
| 106 |
try |
| 107 |
{ |
| 108 |
capture_init(); |
| 109 |
} |
| 110 |
catch(QString error) |
| 111 |
{ |
| 112 |
capture_finished(); |
| 113 |
cout << "CaptureThread: ERROR: " << error.toStdString() << endl; |
| 114 |
m_capture_thread->emitError(error); |
| 115 |
} |
| 116 |
} |
| 117 |
void CaptureThreadImplQt::stopCapture() |
| 118 |
{ |
| 119 |
// cout << "CaptureThreadImplQt::stopCapture" << endl; |
| 120 |
|
| 121 |
try |
| 122 |
{ |
| 123 |
capture_finished(); |
| 124 |
} |
| 125 |
catch(QString error) |
| 126 |
{ |
| 127 |
cout << "CaptureThread: ERROR: " << error.toStdString() << endl; |
| 128 |
m_capture_thread->emitError(error); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void CaptureThreadImplQt::set_params(bool test) { |
| 133 |
Q_UNUSED(test) |
| 134 |
// cout << "CaptureThreadImplQt::set_params " << getASCIISource().toLatin1().constData() << endl; |
| 135 |
|
| 136 |
if(getASCIISource()=="") |
| 137 |
m_audioInputDevice = QAudioDeviceInfo::defaultInputDevice(); |
| 138 |
else { |
| 139 |
|
| 140 |
QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput); |
| 141 |
|
| 142 |
m_audioInputDevice = QAudioDeviceInfo(); |
| 143 |
for(int i=0; m_audioInputDevice.isNull() && i<devices.count(); i++) |
| 144 |
if(getASCIISource()==devices.at(i).deviceName() |
| 145 |
|| m_source==devices.at(i).deviceName()) |
| 146 |
m_audioInputDevice = devices.at(i); |
| 147 |
} |
| 148 |
|
| 149 |
if(m_audioInputDevice.isNull()) |
| 150 |
throw QString("Qt: Cannot open device '")+getASCIISource()+"'"; |
| 151 |
|
| 152 |
QAudioFormat format = m_format; |
| 153 |
|
| 154 |
// TODO Should list all possible parameters ... |
| 155 |
format.setByteOrder(QAudioFormat::LittleEndian); |
| 156 |
format.setCodec("audio/pcm"); |
| 157 |
format.setSampleSize(16); |
| 158 |
format.setSampleType(QAudioFormat::SignedInt); |
| 159 |
|
| 160 |
// Test channels |
| 161 |
format.setSampleRate(44100); // Try with a very common sampling rate |
| 162 |
format.setChannelCount(1); |
| 163 |
if(!m_audioInputDevice.isFormatSupported(format)) { |
| 164 |
QString err_msg = QString("ALSA: cannot set channel count to 1"); |
| 165 |
cout << "CaptureThread: WARNING: " << err_msg.toStdString() << endl; |
| 166 |
format.setChannelCount(2); |
| 167 |
if(!m_audioInputDevice.isFormatSupported(format)) |
| 168 |
throw QString("Qt: Cannot set number of channels to 1 or 2"); |
| 169 |
} |
| 170 |
|
| 171 |
setFormatDescrsAndFns(format.sampleSize()/8, format.sampleType()==QAudioFormat::SignedInt, false, format.channelCount()); |
| 172 |
|
| 173 |
if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX || m_sampling_rate==CaptureThread::SAMPLING_RATE_UNKNOWN) |
| 174 |
{ |
| 175 |
int old_sampling_rate = m_sampling_rate; |
| 176 |
|
| 177 |
cout << "CaptureThread: INFO: Qt: sampling rate set to max or undefined, try to determinate it." << endl; |
| 178 |
|
| 179 |
QList<int> sampling_rates; |
| 180 |
#ifdef Q_OS_WIN |
| 181 |
// The Windows audio backend does not correctly report format support |
| 182 |
// (see QTBUG-9100). Furthermore, although the audio subsystem captures |
| 183 |
// at 11025Hz, the resulting audio is corrupted. |
| 184 |
sampling_rates += 8000; |
| 185 |
#endif |
| 186 |
|
| 187 |
sampling_rates += m_audioInputDevice.supportedSampleRates(); |
| 188 |
|
| 189 |
sampling_rates = QSet<int>(sampling_rates.begin(), sampling_rates.end()).values(); // remove duplicates |
| 190 |
std::sort(sampling_rates.begin(), sampling_rates.end(), std::greater<int>()); |
| 191 |
|
| 192 |
// cout << "Number of sampling rates: " << sampling_rates.count() << endl; |
| 193 |
// int sr; |
| 194 |
// foreach(sr, sampling_rates) |
| 195 |
// cout << sr << endl; |
| 196 |
|
| 197 |
bool foundsr = false; |
| 198 |
while(!foundsr) |
| 199 |
{ |
| 200 |
if(sampling_rates.empty()) |
| 201 |
throw QString("Qt: Cannot set any sample rate"); |
| 202 |
|
| 203 |
m_sampling_rate = sampling_rates.front(); |
| 204 |
cout << "CaptureThread: INFO: Qt: Try sampling rate " << m_sampling_rate << " ..." << flush; |
| 205 |
|
| 206 |
format.setSampleRate(m_sampling_rate); |
| 207 |
|
| 208 |
foundsr = m_audioInputDevice.isFormatSupported(format); |
| 209 |
|
| 210 |
if(!foundsr) cout << " failed" << endl; |
| 211 |
else cout << " success" << endl; |
| 212 |
|
| 213 |
sampling_rates.pop_front(); |
| 214 |
} |
| 215 |
|
| 216 |
if(old_sampling_rate!=m_sampling_rate) |
| 217 |
m_capture_thread->emitSamplingRateChanged(); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
format.setSampleRate(m_sampling_rate); |
| 222 |
if(!m_audioInputDevice.isFormatSupported(format)) |
| 223 |
throw QString("Qt: Cannot set sampling rate"); |
| 224 |
} |
| 225 |
|
| 226 |
if(format != QAudioFormat()) { |
| 227 |
if (m_format != format) { |
| 228 |
m_format = format; |
| 229 |
if(m_audioInput) |
| 230 |
delete m_audioInput; |
| 231 |
m_audioInput = new QAudioInput(m_audioInputDevice, m_format, this); |
| 232 |
m_audioInput->setNotifyInterval(100); |
| 233 |
if(m_audioInput->error()!=QAudio::NoError) |
| 234 |
throw QString("Qt: Cannot create audio input with the given device and format"); |
| 235 |
connect(m_audioInput, SIGNAL(stateChanged(QAudio::State)), |
| 236 |
this, SLOT(audioStateChanged(QAudio::State))); |
| 237 |
// connect(m_audioInput, SIGNAL(notify()), |
| 238 |
// this, SLOT(audioNotify())); |
| 239 |
m_audioInputIODevice = NULL; |
| 240 |
} |
| 241 |
} else { |
| 242 |
throw QString("Qt: cannot set parameters"); |
| 243 |
} |
| 244 |
|
| 245 |
// cout << __FILE__ << ":" << __LINE__ << endl; |
| 246 |
} |
| 247 |
|
| 248 |
void CaptureThreadImplQt::setSamplingRate(int value) |
| 249 |
{ |
| 250 |
// cout << "CaptureThreadImplQt::setSamplingRate " << value << endl; |
| 251 |
|
| 252 |
assert(value>0 || value==CaptureThread::SAMPLING_RATE_MAX); |
| 253 |
|
| 254 |
if(m_sampling_rate!=value || value==CaptureThread::SAMPLING_RATE_MAX) |
| 255 |
{ |
| 256 |
bool was_running = m_capture_thread->isCapturing(); |
| 257 |
if(was_running) m_capture_thread->stopCapture(); |
| 258 |
|
| 259 |
m_sampling_rate = value; |
| 260 |
|
| 261 |
if(m_sampling_rate==CaptureThread::SAMPLING_RATE_MAX) |
| 262 |
{ |
| 263 |
try |
| 264 |
{ |
| 265 |
set_params(true); |
| 266 |
} |
| 267 |
catch(QString error) |
| 268 |
{ |
| 269 |
cout << "CaptureThread: ERROR: " << error.toStdString() << endl; |
| 270 |
m_capture_thread->emitError(error); |
| 271 |
} |
| 272 |
|
| 273 |
// it was just for testing |
| 274 |
capture_finished(); |
| 275 |
} |
| 276 |
else |
| 277 |
m_capture_thread->emitSamplingRateChanged(); |
| 278 |
|
| 279 |
if(was_running) m_capture_thread->startCapture(); |
| 280 |
} |
| 281 |
|
| 282 |
// cout << "~CaptureThreadImplQt::setSamplingRate" << endl; |
| 283 |
} |
| 284 |
|
| 285 |
void CaptureThreadImplQt::audioStateChanged(QAudio::State state) { |
| 286 |
Q_UNUSED(state) |
| 287 |
// cout << "CaptureThreadImplQt::audioStateChanged" << endl; |
| 288 |
|
| 289 |
// if (state==QAudio::IdleState) { |
| 290 |
// stopPlayback(); |
| 291 |
// } else { |
| 292 |
// if (QAudio::StoppedState == state) { |
| 293 |
// // Check error |
| 294 |
// QAudio::Error error = QAudio::NoError; |
| 295 |
// error = m_audioInput->error(); |
| 296 |
// if (QAudio::NoError != error) { |
| 297 |
// reset(); |
| 298 |
// return; |
| 299 |
// } |
| 300 |
// } |
| 301 |
// setState(state); |
| 302 |
// } |
| 303 |
} |
| 304 |
|
| 305 |
void CaptureThreadImplQt::audioDataReady() |
| 306 |
{ |
| 307 |
// cout << "CaptureThreadImplQt::audioDataReady " << m_audioInput->bytesReady() << endl; |
| 308 |
|
| 309 |
if(m_audioInput) { |
| 310 |
m_capture_thread->m_lock.lock(); |
| 311 |
|
| 312 |
const qint64 bytesReady = m_audioInput->bytesReady(); |
| 313 |
|
| 314 |
qint64 nbframes = bytesReady/(m_format.sampleSize()/8); |
| 315 |
|
| 316 |
if(m_capture_thread->m_pause) { |
| 317 |
// Can't find a neater way to do it |
| 318 |
// m_audioInput->reset() blocks the input |
| 319 |
for(qint64 i=0; i<nbframes; i++) { |
| 320 |
long int value = 0; // "Allocate" a "buffer" |
| 321 |
m_audioInputIODevice->read((char*)&value, m_format.sampleSize()/8); |
| 322 |
} |
| 323 |
} |
| 324 |
else { |
| 325 |
|
| 326 |
for(qint64 i=0; i<nbframes; i++) { |
| 327 |
long int value = 0; // "Allocate" a "buffer" |
| 328 |
qint64 bytesRead = m_audioInputIODevice->read((char*)&value, m_format.sampleSize()/8); |
| 329 |
|
| 330 |
if(bytesRead>0) |
| 331 |
addValue(this, decodeValue((void*)&value, 0), i); |
| 332 |
} |
| 333 |
|
| 334 |
m_capture_thread->m_packet_size = nbframes; |
| 335 |
if(m_capture_thread->m_ext_lock) |
| 336 |
{ |
| 337 |
m_capture_thread->m_packet_size_sll = 0; |
| 338 |
m_capture_thread->m_ext_lock = false; |
| 339 |
} |
| 340 |
m_capture_thread->m_packet_size_sll += nbframes; |
| 341 |
} |
| 342 |
|
| 343 |
m_capture_thread->m_lock.unlock(); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
CaptureThreadImplQt::~CaptureThreadImplQt() |
| 348 |
{ |
| 349 |
// cout << "CaptureThreadImplQt::~CaptureThreadImplQt" << endl; |
| 350 |
|
| 351 |
stopCapture(); |
| 352 |
} |
| 353 |
|
| 354 |
#endif |