-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathWavPlayer.cpp
More file actions
112 lines (88 loc) · 2.44 KB
/
WavPlayer.cpp
File metadata and controls
112 lines (88 loc) · 2.44 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
#include <stdio.h>
#include <string.h>
#include "daisy_pod.h"
using namespace daisy;
DaisyPod hardware;
SdmmcHandler sdcard;
FatFSInterface fsi;
WavPlayer wavPlayer;
size_t numberOfFiles;
size_t currentFile = 0;
bool sampleChanged = false;
bool restart = false;
void ProcessControls();
void AudioCallback(AudioHandle::InterleavingInputBuffer in,
AudioHandle::InterleavingOutputBuffer out,
size_t size)
{
ProcessControls();
float sample = 0.0f;
for(size_t i = 0; i < size; i += 2)
{
sample = s162f(wavPlayer.Stream());
out[i] = out[i + 1] = sample * 0.5f;
}
}
void ProcessControls(){
// Debounce digital controls
hardware.ProcessDigitalControls();
// Change file with encoder.
int32_t increment = hardware.encoder.Increment();
if(increment != 0)
{
int next = currentFile + increment;
// Make sure we don't go out of bounds.
next = std::min(next, int (numberOfFiles) - 1);
next = std::max(next, 0);
currentFile = size_t (next);
sampleChanged = true;
}
if(hardware.button1.RisingEdge())
{
restart = true;
}
if(hardware.button2.RisingEdge())
{
wavPlayer.SetLooping(!wavPlayer.GetLooping());
hardware.led2.Set(wavPlayer.GetLooping(),0,0);
}
hardware.UpdateLeds();
}
int main(void)
{
// Init hardware
hardware.Init();
hardware.SetAudioBlockSize(128);
SdmmcHandler::Config sd_cfg;
sd_cfg.Defaults();
sdcard.Init(sd_cfg);
fsi.Init(FatFSInterface::Config::MEDIA_SD);
// Read from the root folder
f_mount(&fsi.GetSDFileSystem(), "/", 1);
wavPlayer.Init(fsi.GetSDPath());
wavPlayer.SetLooping(true);
// Set led1 to indicate Looping status.
float r = wavPlayer.GetLooping();
hardware.led2.Set(r,0,0);
// Init Audio
hardware.StartAdc();
hardware.StartAudio(AudioCallback);
// Loop forever...
while(true)
{
// Prepare buffers for wavPlayer as needed
wavPlayer.Prepare();
numberOfFiles = wavPlayer.GetNumberFiles();
if (sampleChanged)
{
wavPlayer.Close();
wavPlayer.Open(currentFile);
sampleChanged = false;
}
currentFile = wavPlayer.GetCurrentFile();
if (restart){
wavPlayer.Restart();
restart = false;
}
}
}