-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathjamrecorder.cpp
More file actions
617 lines (543 loc) · 20.8 KB
/
jamrecorder.cpp
File metadata and controls
617 lines (543 loc) · 20.8 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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/******************************************************************************\
* Copyright (c) 2020-2026
*
* Author(s):
* pljones
*
******************************************************************************
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/
#include "jamrecorder.h"
using namespace recorder;
/* ********************************************************************************************************
* CJamClient
* ********************************************************************************************************/
/**
* @brief CJamClient::CJamClient
* @param frame Start frame of the client within the session
* @param numChannels 1 for mono, 2 for stereo
* @param name The client's current name
* @param address IP and Port
* @param recordBaseDir Session recording directory
*
* Creates a file for the raw PCM data and sets up a QDataStream to which to write received frames.
* The data is stored Little Endian.
*/
CJamClient::CJamClient ( const qint64 frame, const int _numChannels, const QString name, const CHostAddress& address, const QDir recordBaseDir ) :
startFrame ( frame ),
numChannels ( static_cast<uint16_t> ( _numChannels ) ),
name ( name ),
address ( address ),
out ( nullptr )
{
// At this point we may not have much of a name
QString fileName = ClientName() + "-" + QString::number ( frame ) + "-" + QString::number ( _numChannels );
QString affix = "";
while ( recordBaseDir.exists ( fileName + affix + ".wav" ) )
{
affix = affix.length() == 0 ? "_1" : "_" + QString::number ( affix.remove ( 0, 1 ).toInt() + 1 );
}
fileName = fileName + affix + ".wav";
wavFile = new QFile ( recordBaseDir.absoluteFilePath ( fileName ) );
if ( !wavFile->open ( QFile::OpenMode ( QIODevice::OpenModeFlag::ReadWrite ) ) ) // need to allow rewriting headers
{
throw CGenErr ( "Could not write to WAV file " + wavFile->fileName() );
}
out = new CWaveStream ( wavFile, numChannels );
filename = wavFile->fileName();
}
/**
* @brief CJamClient::Frame Handle a frame of PCM data from a client connected to the server
* @param _name The client's current name
* @param pcm The PCM data
*/
void CJamClient::Frame ( const QString _name, const CVector<int16_t>& pcm, int iServerFrameSizeSamples )
{
name = _name;
for ( int i = 0; i < numChannels * iServerFrameSizeSamples; i++ )
{
*out << pcm[i];
}
frameCount++;
}
/**
* @brief CJamClient::Disconnect Clean up after a disconnected client
*/
void CJamClient::Disconnect()
{
if ( out )
{
static_cast<CWaveStream*> ( out )->finalise();
delete out;
out = nullptr;
}
wavFile->close();
delete wavFile;
wavFile = nullptr;
}
/**
* @brief CJamClient::TranslateChars Replace non-ASCII chars with nearest equivalent, if any, and change all punctuation to _
*/
QString CJamClient::TranslateChars ( const QString& input ) const
{
// Allow letters and numbers
// clang-format off
static const char charmap[256] = {
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '_', '_', '_', '_', '_',
'_', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '_', '_', '_', '_',
'_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '_', '_', '_', '_', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', 'S', '_', 'O', '_', 'Z', '_',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', 's', '_', 'o', '_', 'z', 'Y',
'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_',
'_', '_', '2', '3', '_', 'u', '_', '_', '_', '1', '_', '_', '_', '_', '_', '_',
'A', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I',
'D', 'N', 'O', 'O', 'O', 'O', 'O', 'x', 'O', 'U', 'U', 'U', 'U', 'Y', 'P', 'S',
'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
'd', 'n', 'o', 'o', 'o', 'o', 'o', '_', 'o', 'u', 'u', 'u', 'u', 'y', 'p', 'y'
};
// clang-format on
QByteArray r = input.toLatin1();
for ( auto& c : r )
{
unsigned char uc = c;
c = charmap[uc];
}
return QString::fromLatin1 ( r );
}
/* ********************************************************************************************************
* CJamSession
* ********************************************************************************************************/
/**
* @brief CJamSession::CJamSession Construct a new jam recording session
* @param recordBaseDir The recording base directory
*
* Each session is stored into its own subdirectory of the recording base directory.
*/
CJamSession::CJamSession ( QDir recordBaseDir ) :
sessionDir ( QDir ( recordBaseDir.absoluteFilePath ( "Jam-" + QDateTime().currentDateTimeUtc().toString ( "yyyyMMdd-HHmmsszzz" ) ) ) ),
currentFrame ( 0 ),
chIdDisconnected ( -1 ),
vecptrJamClients ( MAX_NUM_CHANNELS ),
jamClientConnections()
{
QFileInfo fi ( sessionDir.absolutePath() );
fi.setCaching ( false );
if ( !fi.exists() && !QDir().mkpath ( sessionDir.absolutePath() ) )
{
throw CGenErr ( sessionDir.absolutePath() + " does not exist and could not be created" );
}
if ( !fi.isDir() )
{
throw CGenErr ( sessionDir.absolutePath() + " exists but is not a directory" );
}
if ( !fi.isWritable() )
{
throw CGenErr ( sessionDir.absolutePath() + " is a directory but cannot be written to" );
}
// Explicitly set all the pointers to "empty"
vecptrJamClients.fill ( nullptr );
}
/**
* @brief CJamSession::~CJamSession
*/
CJamSession::~CJamSession()
{
// free up any active jamClientConnections
for ( int i = 0; i < jamClientConnections.count(); i++ )
{
if ( jamClientConnections[i] )
{
delete jamClientConnections[i];
jamClientConnections[i] = nullptr;
}
}
}
/**
* @brief CJamSession::DisconnectClient Capture details of the departing client's connection
* @param iChID the channel id of the client that disconnected
*/
void CJamSession::DisconnectClient ( int iChID )
{
vecptrJamClients[iChID]->Disconnect();
jamClientConnections.append ( new CJamClientConnection ( vecptrJamClients[iChID]->NumAudioChannels(),
vecptrJamClients[iChID]->StartFrame(),
vecptrJamClients[iChID]->FrameCount(),
vecptrJamClients[iChID]->ClientName(),
vecptrJamClients[iChID]->FileName() ) );
delete vecptrJamClients[iChID];
vecptrJamClients[iChID] = nullptr;
chIdDisconnected = iChID;
}
/**
* @brief CJamSession::Frame Process a frame emitted for a client by the server
* @param iChID the client channel id
* @param name the client name
* @param address the client IP and port number
* @param numAudioChannels the client number of audio channels
* @param data the frame data
*
* Manages changes that affect how the recording is stored - i.e. if the number of audio channels changes, we need a new file.
* Files are grouped by IP and port number, so if either of those change for a connection, we also start a new file.
*
* Also manages the overall current frame counter for the session.
*/
void CJamSession::Frame ( const int iChID,
const QString name,
const CHostAddress address,
const int numAudioChannels,
const CVector<int16_t> data,
int iServerFrameSizeSamples )
{
if ( iChID == chIdDisconnected )
{
// DisconnectClient has just been called for this channel - this frame is "too late"
chIdDisconnected = -1;
return;
}
if ( vecptrJamClients[iChID] == nullptr )
{
// then we have not seen this client this session
vecptrJamClients[iChID] = new CJamClient ( currentFrame, numAudioChannels, name, address, sessionDir );
}
else if ( numAudioChannels != vecptrJamClients[iChID]->NumAudioChannels() ||
address.InetAddr != vecptrJamClients[iChID]->ClientAddress().InetAddr ||
address.iPort != vecptrJamClients[iChID]->ClientAddress().iPort )
{
DisconnectClient ( iChID );
if ( numAudioChannels == 0 )
{
vecptrJamClients[iChID] = nullptr;
}
else
{
vecptrJamClients[iChID] = new CJamClient ( currentFrame, numAudioChannels, name, address, sessionDir );
}
}
if ( vecptrJamClients[iChID] == nullptr )
{
// Frame allegedly from iChID but unable to establish client details
return;
}
vecptrJamClients[iChID]->Frame ( name, data, iServerFrameSizeSamples );
// If _any_ connected client frame steps past currentFrame, increase currentFrame
if ( vecptrJamClients[iChID]->StartFrame() + vecptrJamClients[iChID]->FrameCount() > currentFrame )
{
currentFrame++;
}
}
/**
* @brief CJamSession::End Clean up any "hanging" clients when the server thinks they all left
*/
void CJamSession::End()
{
for ( int iChID = 0; iChID < vecptrJamClients.size(); iChID++ )
{
if ( vecptrJamClients[iChID] != nullptr )
{
DisconnectClient ( iChID );
vecptrJamClients[iChID] = nullptr;
}
}
}
/**
* @brief CJamSession::Tracks Retrieve a map of (latest) client name to connection items
* @return a map of (latest) client name to connection items
*/
QMap<QString, QList<STrackItem>> CJamSession::Tracks()
{
QMap<QString, QList<STrackItem>> tracks;
for ( int i = 0; i < jamClientConnections.count(); i++ )
{
STrackItem track ( jamClientConnections[i]->Format(),
jamClientConnections[i]->StartFrame(),
jamClientConnections[i]->Length(),
jamClientConnections[i]->FileName() );
if ( !tracks.contains ( jamClientConnections[i]->Name() ) )
{
tracks.insert ( jamClientConnections[i]->Name(), {} );
}
tracks[jamClientConnections[i]->Name()].append ( track );
}
return tracks;
}
/**
* @brief CJamSession::TracksFromSessionDir Replica of CJamSession::Tracks but using the directory contents to construct the track item map
* @param sessionDirName the directory name to scan
* @return a map of (latest) client name to connection items
*/
QMap<QString, QList<STrackItem>> CJamSession::TracksFromSessionDir ( const QString& sessionDirName, int iServerFrameSizeSamples )
{
QMap<QString, QList<STrackItem>> tracks;
const QDir sessionDir ( sessionDirName );
foreach ( auto entry, sessionDir.entryList ( { "*.pcm" } ) )
{
auto split = entry.split ( "." )[0].split ( "-" );
QString name = split[0];
QString hostPort = split[1];
QString frame = split[2];
QString tail = split[3]; // numChannels may have _nnn
QString numChannels = tail.count ( "_" ) > 0 ? tail.split ( "_" )[0] : tail;
QString trackName = name + "-" + hostPort;
if ( !tracks.contains ( trackName ) )
{
tracks.insert ( trackName, {} );
}
QFileInfo fiEntry ( sessionDir.absoluteFilePath ( entry ) );
qint64 length = fiEntry.size() / numChannels.toInt() / iServerFrameSizeSamples;
STrackItem track ( numChannels.toInt(), frame.toLongLong(), length, sessionDir.absoluteFilePath ( entry ) );
tracks[trackName].append ( track );
}
return tracks;
}
/* ********************************************************************************************************
* CJamRecorder
* ********************************************************************************************************/
/**
* @brief CJamRecorder::Init Create recording directory, if necessary, and connect signal handlers
* @param server Server object emitting signals
* @return QString() on success else the failure reason
*/
QString CJamRecorder::Init()
{
QString errmsg = QString();
QFileInfo fi ( recordBaseDir.absolutePath() );
fi.setCaching ( false );
if ( !fi.exists() && !QDir().mkpath ( recordBaseDir.absolutePath() ) )
{
errmsg = QString ( "'%1' does not exist but could not be created." ).arg ( recordBaseDir.absolutePath() );
qCritical() << qUtf8Printable ( errmsg );
return errmsg;
}
if ( !fi.isDir() )
{
errmsg = QString ( "'%1' exists but is not a directory" ).arg ( recordBaseDir.absolutePath() );
qCritical() << qUtf8Printable ( errmsg );
return errmsg;
}
if ( !fi.isWritable() )
{
errmsg = QString ( "'%1' is a directory but cannot be written to" ).arg ( recordBaseDir.absolutePath() );
qCritical() << qUtf8Printable ( errmsg );
return errmsg;
}
return errmsg;
}
/**
* @brief CJamRecorder::Start Start up tasks for a new session
*/
void CJamRecorder::Start()
{
// Ensure any previous cleaning up has been done.
OnEnd();
QString error;
{
// needs to be after OnEnd() as that also locks
QMutexLocker mutexLocker ( &ChIdMutex );
try
{
currentSession = new CJamSession ( recordBaseDir );
isRecording = true;
}
catch ( const CGenErr& err )
{
currentSession = nullptr;
error = err.GetErrorText();
}
}
if ( !currentSession )
{
emit RecordingFailed ( error );
return;
}
emit RecordingSessionStarted ( currentSession->SessionDir().path() );
}
/**
* @brief CJamRecorder::OnEnd Finalise the recording and write the Reaper RPP file
*/
void CJamRecorder::OnEnd()
{
QMutexLocker mutexLocker ( &ChIdMutex );
if ( isRecording )
{
isRecording = false;
currentSession->End();
ReaperProjectFromCurrentSession();
AudacityLofFromCurrentSession();
delete currentSession;
currentSession = nullptr;
}
}
/**
* @brief CJamRecorder::OnTriggerSession End one session and start a new one
*/
void CJamRecorder::OnTriggerSession()
{
// This should magically get everything right...
if ( isRecording )
{
Start();
}
}
/**
* @brief CJamRecorder::OnAboutToQuit End any recording and exit thread
*/
void CJamRecorder::OnAboutToQuit()
{
OnEnd();
QThread::currentThread()->exit();
}
void CJamRecorder::ReaperProjectFromCurrentSession()
{
QString reaperProjectFileName = currentSession->SessionDir().filePath ( currentSession->Name().append ( ".rpp" ) );
const QFileInfo fi ( reaperProjectFileName );
if ( fi.exists() )
{
qWarning() << "CJamRecorder::ReaperProjectFromCurrentSession():" << fi.absolutePath() << "exists and will not be overwritten.";
}
else
{
QFile outf ( reaperProjectFileName );
if ( outf.open ( QFile::WriteOnly ) )
{
QTextStream out ( &outf );
out << CReaperProject ( currentSession->Tracks(), iServerFrameSizeSamples ).toString() << '\n';
qDebug() << "Session RPP:" << reaperProjectFileName;
}
else
{
qWarning() << "CJamRecorder::ReaperProjectFromCurrentSession():" << fi.absolutePath() << "could not be created, no RPP written.";
}
}
}
void CJamRecorder::AudacityLofFromCurrentSession()
{
QString audacityLofFileName = currentSession->SessionDir().filePath ( currentSession->Name().append ( ".lof" ) );
const QFileInfo fi ( audacityLofFileName );
if ( fi.exists() )
{
qWarning() << "CJamRecorder::AudacityLofFromCurrentSession():" << fi.absolutePath() << "exists and will not be overwritten.";
}
else
{
QFile outf ( audacityLofFileName );
if ( outf.open ( QFile::WriteOnly ) )
{
QTextStream sOut ( &outf );
foreach ( auto trackName, currentSession->Tracks().keys() )
{
foreach ( auto item, currentSession->Tracks()[trackName] )
{
QFileInfo fi ( item.fileName );
sOut << "file " << '"' << fi.fileName() << '"';
sOut << " offset " << secondsAt48K ( item.startFrame, iServerFrameSizeSamples ) << '\n';
}
}
sOut.flush();
qDebug() << "Session LOF:" << audacityLofFileName;
}
else
{
qWarning() << "CJamRecorder::AudacityLofFromCurrentSession():" << fi.absolutePath() << "could not be created, no LOF written.";
}
}
}
/**
* @brief CJamRecorder::SessionDirToReaper Replica of CJamRecorder::OnEnd() but using the directory contents to construct the CReaperProject object
* @param strSessionDirName
*
* This is used for testing and is not called from the regular Jamulus code.
*/
void CJamRecorder::SessionDirToReaper ( QString& strSessionDirName, int serverFrameSizeSamples )
{
const QFileInfo fiSessionDir ( QDir::cleanPath ( strSessionDirName ) );
if ( !fiSessionDir.exists() || !fiSessionDir.isDir() )
{
throw CGenErr ( fiSessionDir.absoluteFilePath() + " does not exist or is not a directory. Aborting." );
}
const QDir dSessionDir ( fiSessionDir.absoluteFilePath() );
const QString reaperProjectFileName = dSessionDir.absoluteFilePath ( fiSessionDir.baseName().append ( ".rpp" ) );
const QFileInfo fiRPP ( reaperProjectFileName );
if ( fiRPP.exists() )
{
throw CGenErr ( fiRPP.absoluteFilePath() + " exists and will not be overwritten. Aborting." );
}
QFile outf ( fiRPP.absoluteFilePath() );
if ( !outf.open ( QFile::WriteOnly ) )
{
throw CGenErr ( fiRPP.absoluteFilePath() + " could not be written. Aborting." );
}
QTextStream out ( &outf );
out << CReaperProject ( CJamSession::TracksFromSessionDir ( fiSessionDir.absoluteFilePath(), serverFrameSizeSamples ), serverFrameSizeSamples )
.toString()
<< '\n';
qDebug() << "Session RPP:" << reaperProjectFileName;
}
/**
* @brief CJamRecorder::OnDisconnected Handle disconnection of a client
* @param iChID the client channel id
*/
void CJamRecorder::OnDisconnected ( int iChID )
{
QMutexLocker mutexLocker ( &ChIdMutex );
if ( !isRecording )
{
qWarning() << "CJamRecorder::OnDisconnected: channel" << iChID << "disconnected but not recording";
}
if ( currentSession == nullptr )
{
qWarning() << "CJamRecorder::OnDisconnected: channel" << iChID << "disconnected but no currentSession";
return;
}
currentSession->DisconnectClient ( iChID );
}
/**
* @brief CJamRecorder::OnFrame Handle a frame emitted for a client by the server
* @param iChID the client channel id
* @param name the client name
* @param address the client IP and port number
* @param numAudioChannels the client number of audio channels
* @param data the frame data
*
* Ensures recording has started.
*/
void CJamRecorder::OnFrame ( const int iChID,
const QString name,
const CHostAddress address,
const int numAudioChannels,
const CVector<int16_t> data )
{
// Make sure we are ready
if ( !isRecording )
{
Start();
}
// Start() may have failed, so check again:
if ( !isRecording )
{
return;
}
// needs to be after Start() as that also locks
{
QMutexLocker mutexLocker ( &ChIdMutex );
currentSession->Frame ( iChID, name, address, numAudioChannels, data, iServerFrameSizeSamples );
}
}