-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sine.cpp
128 lines (103 loc) · 3.19 KB
/
Sine.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
#include "Sine.h"
#include "Keyboard.h"
bool Sine::open(PaDeviceIndex index) {
PaStreamParameters outputParameters;
outputParameters.device = index;
if (outputParameters.device == paNoDevice) {
return false;
}
const PaDeviceInfo* pInfo = Pa_GetDeviceInfo(index);
if (pInfo != 0) {
printf("Output device name: '%s'\r", pInfo->name);
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency =
Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaError err = Pa_OpenStream(
&stream_, NULL, /* no input */
&outputParameters, SAMPLE_RATE, paFramesPerBufferUnspecified,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&Sine::paCallback,
this /* Using 'this' for userData so we can cast to Sine* in paCallback method */
);
if (err != paNoError) {
/* Failed to open stream to device !!! */
return false;
}
err = Pa_SetStreamFinishedCallback(stream_, &Sine::paStreamFinished);
if (err != paNoError) {
Pa_CloseStream(stream_);
stream_ = 0;
return false;
}
return true;
}
bool Sine::close() {
if (stream_ == 0)
return false;
PaError err = Pa_CloseStream(stream_);
stream_ = 0;
return (err == paNoError);
}
bool Sine::start() {
if (stream_ == 0)
return false;
PaError err = Pa_StartStream(stream_);
return (err == paNoError);
}
bool Sine::stop() {
if (stream_ == 0)
return false;
PaError err = Pa_StopStream(stream_);
return (err == paNoError);
}
int Sine::paCallbackMethod(const void* inputBuffer, void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags) {
float* out = (float*)outputBuffer;
float sampleRate = 44100.0; // Sample rate
float const frequency(noteFrequency(currentNote_));
// std::cout << "freuquency = " << frequency << "\n";
for (unsigned long i = 0; i < 2 * framesPerBuffer; i= i + 2){
float t = frameCount_ / sampleRate;
out[i]= sin(2 * M_PI * frequency * t); // left channel
// std::cout << "sine" << out[i] << "\n";
out[i+1] = sin(2 * M_PI * frequency * t); // right channel
// std::cout << "sine" << out[i+1] << "\n";
frameCount_++;
}
return paContinue;
}
void Sine::setCurrentNote(Note note) {
currentNote_ = note;
}
void Sine::applyInput(Input input) {
std::visit(InputVisitor{this}, input);
}
void Sine::paStreamFinishedMethod() {
printf("Stream Completed: %s\n", message);
}
void InputVisitor::operator()(InvalidKey) {
return;
}
void InputVisitor::operator()(PianoKey key) {
sine_->currentNote_ =
static_cast<Note>(sine_->octave_ * 12 + static_cast<int>(key));
}
void InputVisitor::operator()(ControlKey key) {
switch (key) {
case ControlKey::OctaveDown: {
sine_->octave_ = sine_->octave_ > 0 ? sine_->octave_ - 1 : 0;
break;
}
case ControlKey::OctaveUp: {
sine_->octave_++;
break;
}
default:
break;
}
}