1+ /* * Generation of a simple Audio signal */
2+ #include " daisy_seed.h"
3+ #include < cmath>
4+
5+ /* * This prevents us from having to type "daisy::" in front of a lot of things. */
6+ using namespace daisy ;
7+
8+ static constexpr float kTargetSr = 48000 .f;
9+ static constexpr size_t kTransferSize = 16384 ;
10+
11+ /* * Global Hardware access */
12+ DaisySeed hw;
13+ SdmmcHandler sdmmc;
14+ FatFSInterface fsi;
15+ WavWriter<kTransferSize > wav_writer;
16+
17+ /* * Basic Fixed-frequency oscillator */
18+ struct SimpleOsc
19+ {
20+ static constexpr float kTargetFreq = 220 .f;
21+ static constexpr float kSignalIncrement
22+ = (M_TWOPI * kTargetFreq ) * (1 .f / kTargetSr );
23+ float phs_;
24+ SimpleOsc () : phs_(0 .f) {}
25+
26+ inline float RenderSample ()
27+ {
28+ float signal = sin (phs_) * 0 .5f ;
29+ phs_ += kSignalIncrement ;
30+ if (phs_ > M_TWOPI)
31+ phs_ -= M_TWOPI;
32+ return signal;
33+ }
34+ };
35+
36+ int main (void )
37+ {
38+ /* * Initialize our hardware */
39+ hw.Init ();
40+
41+ /* * Set up SD Card */
42+ SdmmcHandler::Config sd_cfg;
43+ sd_cfg.Defaults ();
44+ sd_cfg.width = SdmmcHandler::BusWidth::BITS_1;
45+ sdmmc.Init (sd_cfg);
46+ FatFSInterface::Config fsi_cfg;
47+ fsi_cfg.media = FatFSInterface::Config::MEDIA_SD;
48+ fsi.Init (fsi_cfg);
49+ if (f_mount (&fsi.GetSDFileSystem (), " /" , 0 ) != FR_OK)
50+ {
51+ while (1 )
52+ {
53+ hw.SetLed ((System::GetNow () & 127 ) > 63 );
54+ }
55+ }
56+ hw.SetLed (true );
57+
58+ /* * Set up WAV File */
59+ WavWriter<kTransferSize >::Config cfg;
60+ cfg.bitspersample = 16 ;
61+ cfg.channels = 2 ;
62+ cfg.samplerate = kTargetSr ;
63+ wav_writer.Init (cfg);
64+
65+ /* * Prepare to record a 1s 220Hz Audio File */
66+ SimpleOsc oscillator;
67+ size_t duration_sec = 1 ;
68+ size_t duration_in_samps = duration_sec * kTargetSr ;
69+
70+ wav_writer.OpenFile (" ExampleWavFile.wav" );
71+ for (size_t i = 0 ; i < duration_in_samps; i++)
72+ {
73+ // If recording Realtime Audio:
74+ // The rendering/sampling should occur in the realtime audio interrupt
75+ float sample = oscillator.RenderSample ();
76+ float samps_to_write[2 ] = {sample, sample};
77+ wav_writer.Sample (samps_to_write);
78+
79+ // The actual DiskIO should happen outside of the realtime audio interrupt
80+ // For offline-rendering, it is okay to do this check on every sample.
81+ wav_writer.Write ();
82+ }
83+ // Flush and Close
84+ wav_writer.SaveFile ();
85+
86+ while (1 )
87+ {
88+ // Blink Afterwards to show success
89+ hw.SetLed ((System::GetNow () & 511 ) > 255 );
90+ }
91+ }
0 commit comments