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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
/***************************************************************************
Copyright (C) 2011 - Olivier ROUITS <olivier.rouits@free.fr>
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
***************************************************************************/
/**
* @file recorder.cpp
* $Author$
* $Date$
* $Revision$
* @brief Implementation of Recorder class
*/
#include <stdio.h>
#include <math.h>
#include <memory.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include "recorder.h"
#include <QtGlobal>
#include <QWaitCondition>
#include <QProcess>
#include <QFileInfo>
#include <QDateTime>
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
#include <QStorageInfo>
#else
#include <sys/statfs.h>
#endif
//=============================================================================
// Internal defines
//=============================================================================
#define RCD_JK_INPUT_PORTNAME_1 "record_1"
#define RCD_JK_INPUT_PORTNAME_2 "record_2"
#define RCD_WAIT_TIMEOUT_MS 1000
#define RCD_SIG_CHANGE_COUNT 4
#define RCD_VALUE_SIZE sizeof(float)
#define RCD_BUFFER_FRAMES 1024 // 1024 stereo frames
#define RCD_BUFFER_VALUES (2*RCD_BUFFER_FRAMES) // 2 channels per frame
#define RCD_BUFFER_SIZE (RCD_BUFFER_VALUES*RCD_VALUE_SIZE)
#define RCD_RINGBUFFER_FRAMES (64*1024)
#define RCD_RINGBUFFER_VALUES (2*RCD_RINGBUFFER_FRAMES) // 2 channels per frame
#define RCD_RINGBUFFER_SIZE (RCD_RINGBUFFER_VALUES*RCD_VALUE_SIZE)
//=============================================================================
// Jack callback to object calls
//=============================================================================
int jack_process (jack_nframes_t nframes, void *recorder)
{
return ((Recorder*)recorder)->jackProcess(nframes);
}
int jack_sync (jack_transport_state_t state, jack_position_t *pos, void *recorder)
{
return ((Recorder*)recorder)->jackSync(state, pos);
}
void jack_portreg (jack_port_id_t port_id, int reg, void *recorder)
{
((Recorder*)recorder)->jackPortReg(port_id, reg);
}
void jack_shutdown (void *recorder)
{
((Recorder*)recorder)->jackShutdown();
}
//=============================================================================
// Recorder cont/dest methods
//=============================================================================
Recorder::Recorder(QString jackName)
{
this->jackName = jackName;
sndFile = NULL;
outputDir = QDir::home();
currentFilePath = "";
processFilePath = "";
processCmdLine = "";
overruns = 0;
pauseActivationMax = 0;
pauseActivationCount = pauseActivationMax + 1;
shutdown = false;
jackTransMode = true;
jackTransFirstInvocation = true;
jackAutoMode = true;
recordAtLaunch = false;
recording = false;
splitMode = false;
pauseActivationDelay = 3;
pauseLevel = -20;
diskSpace = 0;
leftLevel = 0;
rightLevel = 0;
totalRecordSize = 0;
currentRecordSize = 0;
if ((jackClient = jack_client_open(jackName.toLatin1(), jack_options_t(JackNullOption | JackUseExactName), 0)) == 0) {
throw "Can't start or connect to jack server";
}
sampleRate = jack_get_sample_rate(jackClient);
jackInputPort1 = jack_port_register (jackClient, RCD_JK_INPUT_PORTNAME_1, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
jackInputPort2 = jack_port_register (jackClient, RCD_JK_INPUT_PORTNAME_2, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
jack_set_process_callback(jackClient, jack_process, this);
jack_set_sync_callback(jackClient, jack_sync, this);
jack_on_shutdown (jackClient, jack_shutdown, this);
jack_set_port_registration_callback(jackClient, jack_portreg, this);
jackRingBuffer = jack_ringbuffer_create(RCD_RINGBUFFER_SIZE);
jack_ringbuffer_reset(jackRingBuffer);
currentBuffer = new float[RCD_BUFFER_VALUES];
memset(currentBuffer, 0, RCD_BUFFER_SIZE);
alternateBuffer = new float[RCD_BUFFER_VALUES];
memset(alternateBuffer, 0, RCD_BUFFER_SIZE);
// start jack client
jack_activate(jackClient);
}
Recorder::~Recorder()
{
setShutdown(true);
// wait for recorder thread shutdown
wait(RCD_WAIT_TIMEOUT_MS);
// stop jack client
jack_deactivate(jackClient);
// free / close objects
if (currentBuffer) delete currentBuffer;
if (alternateBuffer) delete alternateBuffer;
if (jackRingBuffer) jack_ringbuffer_free(jackRingBuffer);
if (jackClient) jack_client_close(jackClient);
}
//=============================================================================
// Recorder jack methods
//=============================================================================
int Recorder::jackSync(jack_transport_state_t state, jack_position_t *pos)
{
// at launch, Jack sends its transport state.
// this conflicts with recordAtLaunch feature.
// jackTransFirstInvocation is here to avoid this issue.
if (isJackTransMode() && (!jackTransFirstInvocation || !isRecordAtLaunch())) {
setRecording(state != JackTransportStopped);
}
jackTransFirstInvocation = false;
return isRecordEnabled();
}
int Recorder::jackProcess(jack_nframes_t nframes)
{
int rc = 0;
jack_default_audio_sample_t *in1 =(jack_default_audio_sample_t *)jack_port_get_buffer (jackInputPort1, nframes);
jack_default_audio_sample_t *in2 =(jack_default_audio_sample_t *)jack_port_get_buffer (jackInputPort2, nframes);
// the ringbuffer will transmit data to reorder thread (non RT)
size_t rbspace = jack_ringbuffer_write_space(jackRingBuffer);
if (rbspace < (2*nframes*RCD_VALUE_SIZE)) {
// the ringbuffer is full, IO thread is too late because of IO locks or overloading
overruns++;
rc = 1;
}
else {
// tmp value to be shure of data convertion
float value;
// write interlived stereo data into the ringbuffer
for(jack_nframes_t i = 0; i < nframes; i++) {
value = in1[i];
jack_ringbuffer_write (jackRingBuffer, (const char *)(&value), RCD_VALUE_SIZE);
value = in2[i];
jack_ringbuffer_write (jackRingBuffer, (const char *)(&value), RCD_VALUE_SIZE);
}
}
// wakeup recorder thread because there is data to process
if (dataReadyMutex.tryLock()) {
dataReady.wakeAll();
dataReadyMutex.unlock();
}
return rc;
}
void Recorder::jackPortReg(jack_port_id_t port_id, int reg) {
if (isJackAutoMode() && reg) {
// if a port is registerred, its ID is put in queue for processing by the recorder thread
jackPortRegQueue.enqueue(port_id);
}
}
void Recorder::jackShutdown()
{
setShutdown(true);
}
//=============================================================================
// Recorder public methods
//=============================================================================
//=============================================================================
// Recorder internal methods
//=============================================================================
// The recorder thread run function, all recording algorithm is managed from here
void Recorder::run()
{
int loopCounter = 0;
// this computed attribute must be initialized before entering the main loop
computePauseActivationMax();
// to start always in pause mode if under pause level.
pauseActivationCount = pauseActivationMax + 1;
// record at lanch feature
setRecording(isRecordAtLaunch());
// initial signal for listeners
emit statusChanged();
// the main loop (while shutdown state is off)
while (!isShutdown()) {
// check if therre is some things to do about registration ports (queue not empty)
checkJackAutoConnect();
// while ringbuffer has data.
while (jack_ringbuffer_read_space(jackRingBuffer) >= RCD_BUFFER_SIZE) {
// switch alternate to current buffer and clean it
switchBuffer();
// read the current buffer from ringbuffer
readCurrentBuffer();
// comute levels (DB)
computeCurrentBufferLevels();
// delay may have changed
computePauseActivationMax();
if (isRecording()) {
// a file must be open on theses status values
if (!isFile()) newFile();
if (isPauseLevel()) {
if (pauseActivationCount < pauseActivationMax) {
// the activation delay is not reached, continue to write previous buffer
writeAlternateBuffer();
pauseActivationCount++;
}
else if (pauseActivationCount == pauseActivationMax) {
// the activation delay is reached, fadeout previous buffer to eliminate noises and write it.
fadeoutAlternateBuffer();
writeAlternateBuffer();
if (splitMode) {
// new file will close current file
newFile();
}
pauseActivationCount++;
}
}
else {
if (pauseActivationCount > pauseActivationMax)
// we were in pause satuts, fadein previous buffer to eliminate noises and write it.
fadeinAlternateBuffer();
writeAlternateBuffer();
pauseActivationCount = 0;
}
}
else {
closeFile();
// to re-start always in pause mode.
pauseActivationCount = pauseActivationMax + 1;
}
// update disk space compute
computeDiskSpace();
if (loopCounter >= RCD_SIG_CHANGE_COUNT) {
// notify listeners
emit statusChanged();
loopCounter = 0;
}
else loopCounter++;
}
// wait for new data
dataReady.wait(&dataReadyMutex, RCD_WAIT_TIMEOUT_MS);
}
// to be shure that file is closed
closeFile();
dataReadyMutex.unlock();
}
void Recorder::computePauseActivationMax() {
pauseActivationMax = (sampleRate * pauseActivationDelay ) / RCD_BUFFER_FRAMES;
}
void Recorder::computeCurrentBufferLevels() {
float sumsqr_l = 0;
float sumsqr_r = 0;
int ibuf = 0;
for (int i = 0; i < RCD_BUFFER_FRAMES; i++) {
// 2 channels per frame
sumsqr_l += currentBuffer[ibuf]*currentBuffer[ibuf];
ibuf++;
sumsqr_r += currentBuffer[ibuf]*currentBuffer[ibuf];
ibuf++;
}
float rms_l = sqrtf( sumsqr_l / ((float)RCD_BUFFER_FRAMES) );
float rms_r = sqrtf( sumsqr_r / ((float)RCD_BUFFER_FRAMES) );
float db_l = log10f( rms_l ) * 10;
float db_r = log10f( rms_r ) * 10;
leftLevel = db_l < - 40 ? - 40 : db_l;
rightLevel = db_r < - 40 ? - 40 : db_r;
}
bool Recorder::isRecordEnabled() {
return QFileInfo(outputDir.absolutePath()).isWritable();
}
void Recorder::computeDiskSpace() {
#ifdef QSTORAGEINFO_H
QStorageInfo info(outputDir);
diskSpace = info.isValid() ? 100 - (info.bytesAvailable() * 100) / info.bytesTotal() : -1;
#else
#warning "No QStorageInfo class defined, using old statfs method to compute disk size."
struct statfs stats;
statfs(outputDir.path().toLatin1(), &stats);
diskSpace = 100 - (stats.f_bavail * 100) / stats.f_blocks;
#endif
}
void Recorder::computeFilePath() {
currentFilePath = outputDir.absoluteFilePath(
jackName.toLower()
+ "-" + QDateTime::currentDateTime().toString("yyyy-MM-ddThh-mm-ss")
+ ".wav" );
}
void Recorder::newFile() {
// new file begins always by closing current if exists
closeFile();
SF_INFO sfinfo;
sfinfo.format = SF_FORMAT_WAV|SF_FORMAT_FLOAT;
sfinfo.channels = 2;
sfinfo.samplerate = sampleRate;
sfinfo.frames = RCD_BUFFER_FRAMES;
computeFilePath();
sndFile = sf_open (currentFilePath.toLocal8Bit().constData(), SFM_WRITE, &sfinfo);
}
void Recorder::closeFile() {
// this method is safe if no file is open.
if ( isFile() ) {
sf_close (sndFile);
sndFile = NULL;
// closing file allways involve post processing to start.
processFile();
}
currentFilePath = "";
currentRecordSize = 0;
}
void Recorder::switchBuffer() {
// switch current and alternate buffer
float* tmpBuffer = currentBuffer;
currentBuffer = alternateBuffer;
alternateBuffer = tmpBuffer;
// clean buffer
memset(currentBuffer, 0, RCD_BUFFER_SIZE);
}
void Recorder::readCurrentBuffer() {
// read ringbuffer
jack_ringbuffer_read(jackRingBuffer, (char*)(currentBuffer), RCD_BUFFER_SIZE);
}
void Recorder::writeAlternateBuffer() {
sf_writef_float(sndFile, alternateBuffer, RCD_BUFFER_FRAMES);
totalRecordSize += RCD_BUFFER_SIZE;
currentRecordSize += RCD_BUFFER_SIZE;
}
void Recorder::fadeinAlternateBuffer() {
float gain = 0;
float gaininc = 1 / float(RCD_BUFFER_FRAMES);
int ibuf = 0;
for (int i = 0; i < RCD_BUFFER_FRAMES; i++) {
// 2 channels per frame
alternateBuffer[ibuf++] *= gain;
alternateBuffer[ibuf++] *= gain;
gain += gaininc;
}
}
void Recorder::fadeoutAlternateBuffer() {
float gain = 1;
float gaininc = 1 / float(RCD_BUFFER_FRAMES);
int ibuf = 0;
for (int i = 0; i < RCD_BUFFER_FRAMES; i++) {
// 2 channels per frame
alternateBuffer[ibuf++] *= gain;
alternateBuffer[ibuf++] *= gain;
gain -= gaininc;
}
}
void Recorder::processFile()
{
// only do post process if a command line is defined
if (!processCmdLine.isEmpty() && !currentFilePath.isEmpty()) {
QStringList args;
args.append("-c");
args.append(processCmdLine);
args.append(currentFilePath);
QProcess pr;
pr.startDetached("bash", args);
processFilePath = currentFilePath;
}
}
QString Recorder::getJackConnections(jack_port_t* jackPort) {
const char** connections = NULL;
QString result = "";
if ((connections = jack_port_get_all_connections (jackClient, jackPort)) != NULL) {
for (int i = 0; connections[i]; i++) {
result += QString(connections[i]) + ";";
//printf ("%s\n", connections[i]);
}
jack_free ((void*)connections);
}
return result;
}
void Recorder::setJackConnections(QString cnxLine, jack_port_t* jackPort) {
QStringList strList = cnxLine.split(';', QString::SkipEmptyParts);
for (int i = 0; i < strList.count() ; i++) {
jack_connect(jackClient, strList.at(i).toLocal8Bit().constData(), jack_port_name(jackPort) );
}
}
void Recorder::checkJackAutoConnect() {
while (!jackPortRegQueue.empty()) {
jack_port_t* port = jack_port_by_id(jackClient, jackPortRegQueue.dequeue());
if (jack_port_flags(port) & JackPortIsOutput) {
QString portName = jack_port_name(port);
if (jack_port_connected(jackInputPort1) == 0)
jack_connect(jackClient, portName.toLatin1().constData(), jack_port_name(jackInputPort1) );
else if (jack_port_connected(jackInputPort2) == 0)
jack_connect(jackClient, portName.toLatin1().constData(), jack_port_name(jackInputPort2) );
}
}
}
|