-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMixer.cpp
186 lines (160 loc) · 4.28 KB
/
Mixer.cpp
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
#include "Mixer.hpp"
#include "Channel.hpp"
#include <vector>
namespace tsal {
void Mixer::paStreamFinished(void* userData) {
(void) userData;
return;
}
int Mixer::paCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData ) {
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
Mixer * mixer = static_cast<Mixer *>(userData);
return mixer->audioCallback((float*) outputBuffer, framesPerBuffer);
}
int Mixer::audioCallback(float *outputBuffer, unsigned long frameCount) {
mBuffer.setSize(frameCount, mChannelCount);
mBuffer.clear();
mMaster.getOutput(mBuffer);
for (unsigned long i = 0; i < frameCount; i++) {
mSequencer.tick();
for (unsigned j = 0; j < mChannelCount; j++) {
outputBuffer[i * mChannelCount + j] = mBuffer[i * mChannelCount + j];
}
}
return paContinue;
}
void Mixer::openPaStream() {
mChannelCount = 2;
PaError err = Pa_Initialize();
if (err != paNoError) {
return;
}
PaStreamParameters outputParameters;
const PaDeviceIndex index = Pa_GetDefaultOutputDevice();
outputParameters.device = index;
if (outputParameters.device == paNoDevice) {
printf("No default output device found\n");
return;
}
const PaDeviceInfo* pInfo = Pa_GetDeviceInfo(index);
if (pInfo != 0) {
printf("Output device name: '%s'\n", pInfo->name);
printf("Default sample rate: '%f'\n", pInfo->defaultSampleRate);
}
outputParameters.channelCount = mChannelCount;
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
mSequencer.setSampleRate(mSampleRate);
mSequencer.setBPM(60);
mSequencer.setPPQ(240);
// Add a compressor so people don't break their sound cards
mMaster.add(mCompressor);
err = Pa_OpenStream(&mPaStream,
NULL, /* no input */
&outputParameters,
mSampleRate,
paFramesPerBufferUnspecified,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&Mixer::paCallback,
this
);
if (err != paNoError) {
/* Failed to open stream to device !!! */
return;
}
err = Pa_SetStreamFinishedCallback(mPaStream, &Mixer::paStreamFinished);
if (err != paNoError) {
Pa_CloseStream(mPaStream);
mPaStream = 0;
return;
}
err = Pa_StartStream(mPaStream);
if (err != paNoError) {
return;
}
}
Mixer::Mixer() : mMaster(this), mCompressor(this) {
openPaStream();
}
/**
* @brief Construct a new Mixer
*
* @param sampleRate if left blank, TSAudio will default to the highest sample rate supported
*/
Mixer::Mixer(unsigned sampleRate) : mMaster(this), mCompressor(this) {
mSampleRate = sampleRate;
openPaStream();
}
Mixer::~Mixer() {
PaError err = Pa_CloseStream(mPaStream);
if (err != paNoError) {
return;
}
err = Pa_Terminate();
if (err != paNoError) {
return;
}
}
/**
* @brief Add a channel
*
* @param channel
*/
void Mixer::add(Channel& channel) {
mMaster.add(channel);
}
/**
* @brief Remove a channel
* @param channel
*/
void Mixer::remove(Channel& channel) {
mMaster.remove(channel);
}
/**
* @brief Add an instrument
*
* Add an instrument to the default master channel
*
* @param instrument
*/
void Mixer::add(Instrument& instrument) {
mMaster.add(instrument);
}
/**
* @brief Remove an instrument
*
* Remove an instrument that was added to the default master channel
*
* @param instrument
*/
void Mixer::remove(Instrument& instrument) {
mMaster.remove(instrument);
}
/**
* @brief Add an effect
*
* Add an effect to the default master channel
*
* @param effect
*/
void Mixer::add(Effect& effect) {
mMaster.add(effect);
}
/**
* @brief Remove an effect
*
* Remove an effect that was added to the default master channel
*
* @param effect
*/
void Mixer::remove(Effect& effect) {
mMaster.remove(effect);
}
}