-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmicrobithal_audio.cpp
154 lines (131 loc) · 4.66 KB
/
microbithal_audio.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
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Damien P. George
*
* 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 "main.h"
#include "MicroBitDevice.h"
class AudioSource : public DataSource {
public:
bool started;
DataSink *sink;
ManagedBuffer buf;
void (*callback)(void);
MixerChannel *channel;
AudioSource()
: started(false) {
}
virtual ManagedBuffer pull() {
callback();
return buf;
}
virtual void connect(DataSink& sink_in) {
sink = &sink_in;
}
virtual void disconnect() {
}
virtual int getFormat() {
return DATASTREAM_FORMAT_8BIT_UNSIGNED;
}
};
static AudioSource raw_source;
static AudioSource speech_source;
extern "C" {
#include "microbithal.h"
static uint8_t sound_synth_active_count = 0;
void microbit_hal_audio_select_pin(int pin) {
if (pin < 0) {
uBit.audio.setPinEnabled(false);
} else {
uBit.audio.setPinEnabled(true);
uBit.audio.setPin(*pin_obj[pin]);
}
}
void microbit_hal_audio_select_speaker(bool enable) {
uBit.audio.setSpeakerEnabled(enable);
}
// Input value has range 0-255 inclusive.
void microbit_hal_audio_set_volume(int value) {
uBit.audio.setVolume(value);
}
void microbit_hal_sound_synth_callback(int event) {
if (event == DEVICE_SOUND_EMOJI_SYNTHESIZER_EVT_DONE) {
--sound_synth_active_count;
}
}
bool microbit_hal_audio_is_playing(void) {
return uBit.audio.isPlaying();
}
bool microbit_hal_audio_is_expression_active(void) {
return sound_synth_active_count > 0;
}
void microbit_hal_audio_play_expression(const char *expr) {
++sound_synth_active_count;
uBit.audio.soundExpressions.stop();
// `expr` can be a built-in expression name, or expression data.
// If it's expression data this method parses the data and stores
// it in another buffer ready to play. So `expr` does not need
// to live for the duration of the playing.
uBit.audio.soundExpressions.playAsync(expr);
}
void microbit_hal_audio_stop_expression(void) {
uBit.audio.soundExpressions.stop();
}
void microbit_hal_audio_raw_init(uint32_t sample_rate) {
if (!raw_source.started) {
MicroBitAudio::requestActivation();
raw_source.started = true;
raw_source.callback = microbit_hal_audio_raw_ready_callback;
raw_source.channel = uBit.audio.mixer.addChannel(raw_source, sample_rate, 255);
} else {
raw_source.channel->setSampleRate(sample_rate);
}
}
void microbit_hal_audio_raw_set_rate(uint32_t sample_rate) {
raw_source.channel->setSampleRate(sample_rate);
}
void microbit_hal_audio_raw_write_data(const uint8_t *buf, size_t num_samples) {
if ((size_t)raw_source.buf.length() != num_samples) {
raw_source.buf = ManagedBuffer(num_samples);
}
memcpy(raw_source.buf.getBytes(), buf, num_samples);
raw_source.sink->pullRequest();
}
void microbit_hal_audio_speech_init(uint32_t sample_rate) {
if (!speech_source.started) {
MicroBitAudio::requestActivation();
speech_source.started = true;
speech_source.callback = microbit_hal_audio_speech_ready_callback;
speech_source.channel = uBit.audio.mixer.addChannel(speech_source, sample_rate, 255);
} else {
speech_source.channel->setSampleRate(sample_rate);
}
}
void microbit_hal_audio_speech_write_data(const uint8_t *buf, size_t num_samples) {
if ((size_t)speech_source.buf.length() != num_samples) {
speech_source.buf = ManagedBuffer(num_samples);
}
memcpy(speech_source.buf.getBytes(), buf, num_samples);
speech_source.sink->pullRequest();
}
}