-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathjamcontroller.cpp
More file actions
194 lines (164 loc) · 6.43 KB
/
jamcontroller.cpp
File metadata and controls
194 lines (164 loc) · 6.43 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
/******************************************************************************\
* 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 "jamcontroller.h"
using namespace recorder;
CJamController::CJamController ( CServer* pNServer ) :
pServer ( pNServer ),
bRecorderInitialised ( false ),
bEnableRecording ( false ),
strRecordingDir ( "" ),
pthJamRecorder ( nullptr ),
pJamRecorder ( nullptr )
{}
void CJamController::RequestNewRecording()
{
if ( bRecorderInitialised && bEnableRecording )
{
emit RestartRecorder();
}
}
void CJamController::SetEnableRecording ( bool bNewEnableRecording, bool isRunning )
{
if ( bRecorderInitialised )
{
// message only if the state appears to change
if ( bEnableRecording != bNewEnableRecording )
{
qInfo() << qUtf8Printable ( QString ( "Recording state: %1" ).arg ( bNewEnableRecording ? "enabled" : "disabled" ) );
}
// note that this block executes regardless of whether
// what appears to be a change is being applied, to ensure
// the requested state is the result
bEnableRecording = bNewEnableRecording;
if ( !bEnableRecording )
{
emit StopRecorder();
}
else if ( !isRunning )
{
// This dirty hack is for the GUI. It doesn't care.
emit StopRecorder();
}
}
}
void CJamController::SetRecordingDir ( QString newRecordingDir, int iServerFrameSizeSamples, bool bDisableRecording )
{
if ( bRecorderInitialised && pthJamRecorder != nullptr )
{
// We have a thread and we want to start a new one.
// We only want one running.
// This could take time, unfortunately.
// Hopefully changing recording directory will NOT happen during a long jam...
emit EndRecorderThread();
pthJamRecorder->wait();
delete pthJamRecorder;
pthJamRecorder = nullptr;
}
if ( !newRecordingDir.isEmpty() )
{
pJamRecorder = new recorder::CJamRecorder ( newRecordingDir, iServerFrameSizeSamples );
strRecorderErrMsg = pJamRecorder->Init();
bRecorderInitialised = ( strRecorderErrMsg == QString() );
bEnableRecording = bRecorderInitialised && !bDisableRecording;
qInfo() << qUtf8Printable ( QString ( "Recording state: %1" ).arg ( bEnableRecording ? "enabled" : "disabled" ) );
}
else
{
// This is the only time this is ever true - UI needs to handle it
strRecorderErrMsg = QString();
bRecorderInitialised = false;
bEnableRecording = false;
qInfo() << "Recording state not initialised";
}
if ( bRecorderInitialised )
{
strRecordingDir = newRecordingDir;
pthJamRecorder = new QThread();
pthJamRecorder->setObjectName ( "JamRecorder" );
pJamRecorder->moveToThread ( pthJamRecorder );
// QT signals
QObject::connect ( pthJamRecorder, &QThread::finished, pJamRecorder, &QObject::deleteLater );
QObject::connect ( QCoreApplication::instance(),
&QCoreApplication::aboutToQuit,
pJamRecorder,
&CJamRecorder::OnAboutToQuit,
Qt::ConnectionType::BlockingQueuedConnection );
// from the controller to the recorder
QObject::connect ( this, &CJamController::RestartRecorder, pJamRecorder, &CJamRecorder::OnTriggerSession );
QObject::connect ( this, &CJamController::StopRecorder, pJamRecorder, &CJamRecorder::OnEnd );
QObject::connect ( this,
&CJamController::EndRecorderThread,
pJamRecorder,
&CJamRecorder::OnAboutToQuit,
Qt::ConnectionType::BlockingQueuedConnection );
// from the server to the recorder
QObject::connect ( this, &CJamController::Stopped, pJamRecorder, &CJamRecorder::OnEnd );
QObject::connect ( this, &CJamController::ClientDisconnected, pJamRecorder, &CJamRecorder::OnDisconnected );
qRegisterMetaType<CVector<int16_t>> ( "CVector<int16_t>" );
QObject::connect ( this, &CJamController::AudioFrame, pJamRecorder, &CJamRecorder::OnFrame );
// from the recorder to the server
QObject::connect ( pJamRecorder, &CJamRecorder::RecordingSessionStarted, this, &CJamController::RecordingSessionStarted );
// from the recorder to the controller
QObject::connect ( pJamRecorder, &CJamRecorder::RecordingFailed, this, &CJamController::OnRecordingFailed );
pthJamRecorder->start ( QThread::NormalPriority );
}
else
{
strRecordingDir = "";
}
}
void CJamController::OnRecordingFailed ( QString error )
{
if ( !bEnableRecording )
{
// Recording has already been stopped, possibly
// by a previous OnRecordingFailed call from another client/thread.
return;
}
strRecorderErrMsg = error;
qWarning() << "Could not start recording:" << error;
// Turn off recording until it is manually re-enabled via UI or signals.
// This needs to be done from the CServer side to cover all relevant
// state.
pServer->SetEnableRecording ( false );
}
ERecorderState CJamController::GetRecorderState()
{
// return recorder state
if ( bRecorderInitialised )
{
if ( bEnableRecording )
{
return RS_RECORDING;
}
else
{
return RS_NOT_ENABLED;
}
}
else
{
return RS_NOT_INITIALISED;
}
}