-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSine.h
112 lines (88 loc) · 2.78 KB
/
Sine.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
#ifndef SINE_H
#define SINE_H
#include "Keyboard.h"
#include "model/Note.h"
#include "portaudio.h"
#include <math.h>
#include <stdio.h>
#include <iostream>
#define NUM_SECONDS (5)
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (64)
#ifndef M_PI
#define M_PI (3.14159265)
#endif
#define TABLE_SIZE (200)
class Sine;
struct InputVisitor {
InputVisitor(Sine* sine) : sine_{sine} {}
void operator()(InvalidKey);
void operator()(PianoKey key);
void operator()(ControlKey key);
Sine* sine_;
};
class Sine {
public:
Sine() : stream_(0), left_phase(0), right_phase(0) {
/* initialise sinusoidal wavetable */
for (int i = 0; i < TABLE_SIZE; i++) {
sine[i] = (float)sin(((double)i / (double)TABLE_SIZE) * M_PI * 2.);
}
sprintf(message, "No Message");
}
bool open(PaDeviceIndex index);
bool close();
bool start();
bool stop();
void setCurrentNote(Note note);
void applyInput(Input input);
private:
friend class InputVisitor;
/* The instance callback, where we have access to every method/variable in object of class Sine */
int paCallbackMethod(const void* inputBuffer, void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags);
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int paCallback(const void* inputBuffer, void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void* userData) {
/* Here we cast userData to Sine* type so we can call the instance method paCallbackMethod, we can do that since
we called Pa_OpenStream with 'this' for userData */
return ((Sine*)userData)
->paCallbackMethod(inputBuffer, outputBuffer, framesPerBuffer, timeInfo,
statusFlags);
}
void paStreamFinishedMethod();
/*
* This routine is called by portaudio when playback is done.
*/
static void paStreamFinished(void* userData) {
return ((Sine*)userData)->paStreamFinishedMethod();
}
PaStream* stream_;
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
char message[20];
size_t frameCount_{};
Note currentNote_{Note::A4};
uint octave_{3};
};
class ScopedPaHandler {
public:
ScopedPaHandler() : _result(Pa_Initialize()) {}
~ScopedPaHandler() {
if (_result == paNoError) {
Pa_Terminate();
}
}
PaError result() const { return _result; }
private:
PaError _result;
};
#endif // SINE_H