-
Notifications
You must be signed in to change notification settings - Fork 596
/
recording.cpp
213 lines (186 loc) · 4.35 KB
/
recording.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
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
/*
The MIT License (MIT)
Copyright (c) 2022 Lancaster University
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "pxt.h"
#include "MicroBit.h"
#if MICROBIT_CODAL
#include "StreamRecording.h"
#endif
using namespace pxt;
namespace record {
#if MICROBIT_CODAL
static StreamRecording *recording = NULL;
static SplitterChannel *splitterChannel = NULL;
static MixerChannel *channel = NULL;
#endif
void checkEnv(int sampleRate = -1) {
#if MICROBIT_CODAL
if (recording == NULL) {
if (sampleRate == -1)
sampleRate = 11000;
MicroBitAudio::requestActivation();
splitterChannel = uBit.audio.splitter->createChannel();
recording = new StreamRecording(*splitterChannel);
channel = uBit.audio.mixer.addChannel(*recording, sampleRate);
channel->setVolume(75.0);
uBit.audio.mixer.setVolume(1000);
uBit.audio.setSpeakerEnabled(true);
}
#endif
}
/**
* Record an audio clip
*/
//% promise
void record() {
#if MICROBIT_CODAL
checkEnv();
recording->recordAsync();
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
/**
* Play the audio clip that is saved in the buffer
*/
//%
void play() {
#if MICROBIT_CODAL
checkEnv();
recording->playAsync();
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
/**
* Stop recording
*/
//%
void stop() {
#if MICROBIT_CODAL
checkEnv();
recording->stop();
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
/**
* Clear the buffer
*/
//%
void erase() {
#if MICROBIT_CODAL
checkEnv();
recording->erase();
#endif
}
/**
* Set sensitity of the microphone input
*/
//%
void setMicrophoneGain(float gain) {
#if MICROBIT_CODAL
uBit.audio.processor->setGain(gain);
#endif
}
/**
* Get how long the recorded audio clip is
*/
//%
int audioDuration(int sampleRate) {
#if MICROBIT_CODAL
return recording->duration(sampleRate);
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
return MICROBIT_NOT_SUPPORTED;
#endif
}
/**
* Get whether the playback is active
*/
//%
bool audioIsPlaying() {
#if MICROBIT_CODAL
return recording->isPlaying();
#else
return false;
#endif
}
/**
* Get whether the microphone is listening
*/
//%
bool audioIsRecording() {
#if MICROBIT_CODAL
return recording->isRecording();
#else
return false;
#endif
}
/**
* Get whether the board is recording or playing back
*/
//%
bool audioIsStopped() {
#if MICROBIT_CODAL
return recording->isStopped();
#else
return false;
#endif
}
/**
* Change the sample rate of the splitter channel (audio input)
*/
//%
void setInputSampleRate(int sampleRate) {
#if MICROBIT_CODAL
checkEnv();
splitterChannel->requestSampleRate(sampleRate);
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
/**
* Change the sample rate of the mixer channel (audio output)
*/
//%
void setOutputSampleRate(int sampleRate) {
#if MICROBIT_CODAL
if (recording == NULL) {
checkEnv(sampleRate);
} else {
channel->setSampleRate(sampleRate);
}
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
/**
* Set the sample rate for both input and output
*/
//%
void setBothSamples(int sampleRate) {
#if MICROBIT_CODAL
setOutputSampleRate(sampleRate);
splitterChannel->requestSampleRate(sampleRate);
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
} // namespace record