|
| 1 | +/** Simple example of WavParser class |
| 2 | + * |
| 3 | + * Prints out some information about the WAV files on |
| 4 | + * an attached SD card. |
| 5 | + * |
| 6 | + * To run this: |
| 7 | + * 1. Put some WAV files on an SD Card |
| 8 | + * (This program will look at the first four it finds). |
| 9 | + * 2. Program the Daisy with this example, and the SD Card connected. |
| 10 | + * 3. Connect to the Daisy via USB Serial |
| 11 | + * 4. A list of the files found with some audio info will be output |
| 12 | + */ |
| 13 | +#include "daisy_seed.h" |
| 14 | + |
| 15 | +using namespace daisy; |
| 16 | + |
| 17 | +static constexpr const size_t kMaxFiles = 4; |
| 18 | + |
| 19 | +static DaisySeed hw; |
| 20 | +static SdmmcHandler sdmmc; |
| 21 | +static FatFSInterface fsi; |
| 22 | +static FileTable<kMaxFiles> file_table; |
| 23 | +static FIL file; |
| 24 | + |
| 25 | +int main(void) |
| 26 | +{ |
| 27 | + /** Initialize our hardware */ |
| 28 | + hw.Init(true); |
| 29 | + hw.StartLog(true); |
| 30 | + |
| 31 | + /** SD Card / FatFS Interface Init */ |
| 32 | + SdmmcHandler::Config sdcfg; |
| 33 | + sdcfg.Defaults(); |
| 34 | + sdcfg.speed = SdmmcHandler::Speed::STANDARD; |
| 35 | + sdcfg.width = SdmmcHandler::BusWidth::BITS_1; |
| 36 | + sdmmc.Init(sdcfg); |
| 37 | + fsi.Init(FatFSInterface::Config::Media::MEDIA_SD); |
| 38 | + f_mount(&fsi.GetSDFileSystem(), "/", 1); |
| 39 | + |
| 40 | + /** We'll fill up the table with WAV files and |
| 41 | + * then parse those, and present some info. |
| 42 | + */ |
| 43 | + file_table.Fill("/", ".wav"); |
| 44 | + |
| 45 | + |
| 46 | + if(file_table.GetNumFiles() > 0) |
| 47 | + { |
| 48 | + for(size_t i = 0; i < file_table.GetNumFiles(); i++) |
| 49 | + { |
| 50 | + auto sta = f_open( |
| 51 | + &file, file_table.GetFileName(i), (FA_OPEN_EXISTING | FA_READ)); |
| 52 | + if(sta != FR_OK) |
| 53 | + { |
| 54 | + hw.PrintLine("Could not open: %s", file_table.GetFileName(i)); |
| 55 | + continue; |
| 56 | + } |
| 57 | + FileReader reader(&file); |
| 58 | + WavParser parser; |
| 59 | + if(!parser.parse(reader)) |
| 60 | + { |
| 61 | + hw.PrintLine("Error parsing file: %s", |
| 62 | + file_table.GetFileName(i)); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + const auto& info = parser.info(); |
| 67 | + hw.PrintLine("File Information: %s", file_table.GetFileName(i)); |
| 68 | + hw.PrintLine("\tSample Rate:\t%d", info.sampleRate); |
| 69 | + hw.PrintLine("\tChannels:\t%d", info.numChannels); |
| 70 | + hw.PrintLine("\tBit Depth:\t%d", info.bitsPerSample); |
| 71 | + |
| 72 | + /** File Duration in seconds */ |
| 73 | + size_t dur_samples |
| 74 | + = parser.dataSize() |
| 75 | + / ((info.bitsPerSample / 8) * info.numChannels); |
| 76 | + float dur_seconds = static_cast<float>(dur_samples) |
| 77 | + / static_cast<float>(info.sampleRate); |
| 78 | + float frac = dur_seconds - static_cast<int>(dur_seconds); |
| 79 | + hw.PrintLine("\tDuration (seconds):\t%d.%02d", |
| 80 | + static_cast<size_t>(dur_seconds), |
| 81 | + static_cast<size_t>(frac * 100.f)); |
| 82 | + |
| 83 | + /** Number of metadata chunks */ |
| 84 | + hw.PrintLine("\tMetaData Chunks:\t%d", parser.metadataCount()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + while(1) {} |
| 90 | +} |
0 commit comments