Skip to content

Commit 2c814cf

Browse files
fixed styling, and added WavParser example.
1 parent e98848e commit 2c814cf

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

examples/WavParser/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set(FIRMWARE_NAME WavParser)
2+
set(FIRMWARE_SOURCES main.cpp)
3+
include(DaisyProject)

examples/WavParser/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Project Name
2+
TARGET = WavParser
3+
4+
# Sources
5+
CPP_SOURCES = main.cpp
6+
7+
DEBUG=0
8+
OPT=-O3
9+
10+
USE_FATFS=1
11+
12+
# Library Locations
13+
LIBDAISY_DIR = ../..
14+
15+
# Core location, and generic Makefile.
16+
SYSTEM_FILES_DIR = $(LIBDAISY_DIR)/core
17+
include $(SYSTEM_FILES_DIR)/Makefile

examples/WavParser/main.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

src/util/FileTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FileTable
4040
*
4141
* @return true if files are loaded, otherwise false
4242
*/
43-
bool Fill(const char *path, const char *endswith=nullptr)
43+
bool Fill(const char *path, const char *endswith = nullptr)
4444
{
4545
FRESULT res = FR_OK;
4646
if(path == nullptr)

0 commit comments

Comments
 (0)