-
Notifications
You must be signed in to change notification settings - Fork 1
/
musebox.h
269 lines (241 loc) · 7.19 KB
/
musebox.h
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
#ifndef MUSEBOX_H
#define MUSEBOX_H
#include <QObject>
#include "DSP/track.h"
#include "audioconfigurationdialog.h"
#include "hardware.h"
#include <QDebug>
#include <QMainWindow>
#include <DataModel/trackmodel.h>
#include "newprojectdialog.h"
#include "exportaudiodialog.h"
#include <QFileDialog>
#include <sndfile.h>
//Main MuseBox class. Handles Track addition/remove logic, interaction between audio engine and GUI, and file load/save.
class MuseBox : public QMainWindow
{
Q_OBJECT
public:
explicit MuseBox(QWidget *parent = 0);
//Project related
void NewProject();
void LoadProject(QString path);
void SaveProject(QString path);
//Track manipulation
void AddTrack();
void RemoveTrack(int idx);
void MoveTrack(int from, int to);
Track* GetTrack(int idx);
bool playStateBeforeMove;//Used to recover play state after a FF/RW action
TrackModel trackModel;
Q_INVOKABLE void resetAudio()
{
Hardware::ResetAudio();
}
Q_INVOKABLE void startAudio()
{
Hardware::StartAudio();
}
Q_INVOKABLE void stopAudio()
{
Hardware::StopAudio();
}
Q_INVOKABLE void openConfigurationDialog() {
qDebug()<<"Opening dialog...";
AudioConfigurationDialog* dialog = new AudioConfigurationDialog();
dialog->show();
}
void onExit()
{
Hardware::SaveConfig();
exit(0);//Surprise!!! LEAK LEAK LEAK!
Hardware::DeInit();
}
Q_INVOKABLE void play()
{
HWLOCK;
Hardware::TransposeMachine->Play();
}
Q_INVOKABLE void stop()
{
HWLOCK;
Hardware::TransposeMachine->Stop();
}
Q_INVOKABLE void setRecord(bool flag)
{
HWLOCK;
Hardware::TransposeMachine->SetRecord(flag);
}
Q_INVOKABLE void togglePlayStop()
{
HWLOCK;
Hardware::TransposeMachine->TogglePlayStop();
}
Q_INVOKABLE void moveStart()
{
HWLOCK;
playStateBeforeMove = Hardware::TransposeMachine->Playing;
Hardware::TransposeMachine->Play();
}
Q_INVOKABLE void moveEnd()
{
HWLOCK;
if(!playStateBeforeMove)
Hardware::TransposeMachine->Stop();
}
Q_INVOKABLE void setSpeed(float speed)
{
HWLOCK;
Hardware::TransposeMachine->Speed = speed;
}
Q_INVOKABLE void setClick(bool click)
{
HWLOCK;
Hardware::TransposeMachine->Click = click;
}
Q_INVOKABLE void setLoop(bool loop)
{
HWLOCK;
Hardware::TransposeMachine->Loop = loop;
}
Q_INVOKABLE void beginUpdateGUI()
{
Hardware::Lock();
}
Q_INVOKABLE void endUpdateGUI()
{
Hardware::Unlock();
}
Q_INVOKABLE float query_dBL(int channel)
{
return Hardware::MainMixer->InputMixerChannels[channel]->dbL();
}
Q_INVOKABLE float query_dBR(int channel)
{
return Hardware::MainMixer->InputMixerChannels[channel]->dbR();
}
Q_INVOKABLE float masterL()
{
return Hardware::MasterDb(0);
}
Q_INVOKABLE float masterR()
{
return Hardware::MasterDb(1);
}
//posOnGUI is measured in pixels
Q_INVOKABLE void setCurrentPosition(int posOnGUI)
{
HWLOCK;
Hardware::TransposeMachine->setCurrentPosition(posOnGUI);
}
Q_INVOKABLE void setLoopStart(int posOnGUI)
{
HWLOCK;
Hardware::TransposeMachine->setLoopStart(posOnGUI);
}
Q_INVOKABLE void setLoopEnd(int posOnGUI)
{
HWLOCK;
Hardware::TransposeMachine->setLoopEnd(posOnGUI);
}
Q_INVOKABLE QString saveProject(QString filename = "")
{
if(filename == ""){
filename = QFileDialog::getSaveFileName(this, "Save to...",QString(),"*.mb",NULL,0);
}
if(filename != "")
{
trackModel.saveFile(filename);
}
return filename;
}
Q_INVOKABLE QString loadProject(QString filename = "")
{
if(filename == ""){
filename = QFileDialog::getOpenFileName(this, "Open...",QString(),"*.mb",NULL,0);
}
if(filename != "")
{
trackModel.clear();
trackModel.loadFile(filename);
}
return filename;
}
Q_INVOKABLE void clearProject()
{
trackModel.clear();
}
Q_INVOKABLE void newProject()
{
NewProjectDialog dialog;
if( QDialog::Accepted == dialog.exec())
{
trackModel.clear();
Hardware::TransposeMachine->beatCount = dialog.beatCount;
Hardware::TransposeMachine->unitCount = dialog.unitCount;
Hardware::TransposeMachine->SetBPM(dialog.bpm);
}
}
Q_INVOKABLE void exportSong(){
Hardware::StopAudio();
ExportAudioDialog dialog(this);
if(QDialog::Accepted != dialog.exec())
return;
int loopStart,loopEnd,time;
loopStart = Hardware::TransposeMachine->LoopStart;
loopEnd = Hardware::TransposeMachine->LoopEnd;
time = Hardware::TransposeMachine->Time;
short* dataFrame = new short[2];
SF_INFO info;
info.channels=2;
info.samplerate = Hardware::SampleRate;
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
SNDFILE* file = sf_open(dialog.filename().toLocal8Bit().data(),SFM_WRITE,&info);
for(int i=0;i<Hardware::AudioInput->OutputChannelCount;++i)
{
Hardware::AudioInput->WriteOutput(i,0);
}
Hardware::TransposeMachine->Play();
if(dialog.isSong()){
int SamplePerBar = Hardware::TransposeMachine->BeatsToSample(Hardware::TransposeMachine->beatCount);
int startPosition = SamplePerBar * dialog.fromBar();
int endPosition = SamplePerBar * (dialog.toBar() + 1);
Hardware::TransposeMachine->Time = startPosition;
Hardware::TransposeMachine->LoopEnd = startPosition - 1;//To prevent looping
for(int i=startPosition;i < endPosition;++i)
{
Hardware::Update();
dataFrame[0] = Hardware::AudioOutput->ReadInput(0) * 32767;
dataFrame[1] = Hardware::AudioOutput->ReadInput(1) * 32767;
sf_write_short(file,dataFrame,2);
}
}else{
Hardware::TransposeMachine->Time = loopStart;
for(int i=0;i<dialog.loopCount();++i){
for(int j=0;j<(loopEnd - loopStart+1);++j)
{
Hardware::Update();
dataFrame[0] = Hardware::AudioOutput->ReadInput(0) * 32767;
dataFrame[1] = Hardware::AudioOutput->ReadInput(1) * 32767;
sf_write_short(file,dataFrame,2);
}
}
}
sf_close(file);
Hardware::TransposeMachine->Stop();
qDebug()<<"File write complete!";
Hardware::TransposeMachine->LoopStart = loopStart;
Hardware::TransposeMachine->LoopEnd = loopEnd;
Hardware::TransposeMachine->Time = time;
Hardware::StartAudio();
delete[] dataFrame;
}
virtual void closeEvent(QCloseEvent *event)
{
onExit();
QMainWindow::closeEvent(event);
}
signals:
public slots:
};
#endif // MUSEBOX_H