-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWavPlayer.cpp
More file actions
184 lines (168 loc) · 4.57 KB
/
WavPlayer.cpp
File metadata and controls
184 lines (168 loc) · 4.57 KB
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
// # WavPlayer
// ## Description
// Fairly simply sample player.
// Loads 16
//
// Play .wav file from the SD Card.
//
#include "WavPlayer.h"
int main(void)
{
// Init hardware
hw.Init();
hw.seed.usb_handle.Init(UsbHandle::FS_BOTH);
midi_cfg.transport_config.periph = MidiUsbTransport::Config::EXTERNAL;
midi.Init(midi_cfg);
System::Delay(250);
samplePool.Init(&(_pool[0]));
// hw.ClearLeds();
SdmmcHandler::Config sd_cfg;
sd_cfg.Defaults();
sdcard.Init(sd_cfg);
fsi.Init(FatFSInterface::Config::MEDIA_SD);
f_mount(&fsi.GetSDFileSystem(), "/", 1);
fileSystemHandler.Init(fsi.GetSDPath());
// Load the first couple samples into ram
PreloadVoices();
//
streamer.Init(&fileSystemHandler);
streamer.SetFile(0);
// streamer.Init(fsi.GetSDPath());
// streamer.SetLooping(true);
// SET LED to indicate Looping status.
//hw.SetLed(DaisyPatch::LED_2_B, streamer.GetLooping());
// Init Audio
hw.SetAudioBlockSize(blocksize);
hw.SetAudioSampleRate(SaiHandle::Config::SampleRate::SAI_48KHZ);
hw.StartAudio(AudioCallback);
// hw.midi.Init();
// hw.midi.StartReceive();
// Loop forever...
for(;;)
{
streamer.NextFrame();
// TODO: get next frame of samples by DMA
// streamer.Prepare();
// Debounce digital controls
hw.ProcessDigitalControls();
// Get Midi Messages
// hw.midi.Listen();
midi.Listen();
// Handle MIDI Events
while(midi.HasEvents()) // while(hw.midi.HasEvents())
{
HandleMidiMessage(midi.PopEvent()); // HandleMidiMessage(hw.midi.PopEvent());
}
// Handle Hardware Inputs
HandleHardwareInputs();
// Update UI Elements
UpdateUI();
}
}
void AudioCallback(AudioHandle::InterleavingInputBuffer in,
AudioHandle::InterleavingOutputBuffer out,
size_t size)
{
memset(&(scratch[0]), 0, frameSize*sizeof(float));
streamer.Process(scratch, frameSize);
sampler.Process(scratch, frameSize);
for(size_t i = 0, j = 0; i < size; i += 2, ++j)
{
out[i] = out[i + 1] = scratch[j] * 0.5f;
}
}
// Typical Switch case for Message Type.
void HandleMidiMessage(MidiEvent m)
{
switch(m.type)
{
case NoteOn:
{
NoteOnEvent p = m.AsNoteOn();
DebugPrintMidiEvent(m);
// This is to avoid Max/MSP Note outs for now..
if(m.data[1] != 0)
{
size_t targetChannel = m.channel;
sampler.Play(targetChannel);
// streamer.Play();
// p = m.AsNoteOn();
// osc.SetFreq(mtof(p.note));
// osc.SetAmp((p.velocity / 127.0f));
}
break;
}
case NoteOff:
{
size_t targetChannel = m.channel;
sampler.Stop(targetChannel);
// streamer.Stop();
break;
}
case ControlChange:
{
ControlChangeEvent p = m.AsControlChange();
switch(p.control_number)
{
case 1:
// CC 1 for cutoff.
// filt.SetFreq(mtof((float)p.value));
streamer.NextFile();
break;
case 2:
// CC 2 for res.
// filt.SetRes(((float)p.value / 127.0f));
streamer.PreviousFile();
break;
default: break;
}
break;
}
default: break;
}
}
void HandleHardwareInputs()
{
// Change file with encoder.
uint32_t inc = hw.encoder.Increment();
if(inc > 0)
{
streamer.NextFile();
}
else if(inc < 0)
{
streamer.PreviousFile();
}
if(hw.button1.RisingEdge())
{
streamer.Play();
}
if(hw.button2.RisingEdge())
{
streamer.SetLooping(!streamer.GetLooping());
//hw.SetLed(DaisyPod::LED_2_B, sampler.GetLooping());
//dsy_gpio_write(&hw.leds[DaisyPatch::LED_2_B],
// static_cast<uint8_t>(!sampler.GetLooping()));
if(streamer.GetLooping())
{
hw.led2.Set(0.f, 1.f, 0.f);
}
else
{
hw.led2.Set(1.f, 0.f, 0.f);
}
}
}
void UpdateUI()
{
if(!streamer.GetLooping())
{
hw.led1.Set(1.f, 0.f, 0.f);
}
else
{
hw.led1.Set(0.f, 1.f, 0.f);
}
hw.led1.Update();
hw.led2.Update();
}