Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix low notes; clean code #7

Merged
merged 18 commits into from
May 11, 2022
Prev Previous commit
Next Next commit
spring cleaning
  • Loading branch information
cvonk committed May 8, 2022
commit 8d882a96cc8b09381295490c49864cc9ec84c618
14 changes: 7 additions & 7 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ namespace {
noteName[1] = 'b';
}

sampleCnt_t nrOfSamplesInFile;
sample_cnt_t nrOfSamplesInFile;

if (Wave::readHeader(f, &nrOfSamplesInFile) != 0 ||
Wave::readSamples(f, CONFIG_MIDIMIKE_SAMPLE_RATE * CONFIG_MIDIMIKE_FILE_SEC2SKIP, NULL) != 0 ||
Wave::readSamples(f, CONFIG_MIDIMIKE_WINDOW_SIZE, samples) != 0) {
if (wave_read_hdr(f, &nrOfSamplesInFile) != 0 ||
wave_read_samples(f, CONFIG_MIDIMIKE_SAMPLE_RATE * CONFIG_MIDIMIKE_FILE_SEC2SKIP, NULL) != 0 ||
wave_read_samples(f, CONFIG_MIDIMIKE_WINDOW_SIZE, samples) != 0) {

return 2;
}

// determine peak-to-peak value
sample_t min = SCHAR_MAX;
sample_t max = SCHAR_MIN;
for (sampleCnt_t ii = 0; ii < CONFIG_MIDIMIKE_WINDOW_SIZE; ii++) {
for (sample_cnt_t ii = 0; ii < CONFIG_MIDIMIKE_WINDOW_SIZE; ii++) {
sample_t const s = samples[ii];
if (s < min) {
min = s;
Expand Down Expand Up @@ -222,7 +222,7 @@ setup()

//Serial.println("setup()");
#if SRC == SRC_FILE
if (SdDir::begin(SPI_SD_CS) != 0) {
if (sddir_init(SPI_SD_CS) != 0) {
Serial.println("No SD Card"); // uSD card inserted/formatted ??
}
#endif
Expand Down Expand Up @@ -334,6 +334,6 @@ loop()
char dir[13] = "/notes/\012345";
utoa(CONFIG_MIDIMIKE_SAMPLE_RATE, &dir[7], 10);
//Serial.println(dir);
SdDir::forEachFile(dir, _calcNoteFromFile);
sddir_for_each_file(dir, _calcNoteFromFile);
#endif
}
2 changes: 1 addition & 1 deletion main/sample_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

typedef int8_t sample_t; // one sample
typedef sample_t * samples_t; // chunk containing samples
typedef uint_least16_t sampleCnt_t; // number of samples in chunk
typedef uint_least16_t sample_cnt_t; // number of samples in chunk
typedef uint_least16_t sampleRate_t; // sample rate in samples/sec

typedef uint8_t amplitude_t; // signal amplitude
Expand Down
1 change: 0 additions & 1 deletion main/src/display/pianoroll.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Arduino.h>
#include <stdint.h>

#include "../../segment_t.h"

void pianoroll_init(uint_least8_t tftCS_pin, uint_least8_t dc_pin, uint_least8_t reset_pin);
Expand Down
1 change: 0 additions & 1 deletion main/src/display/staff.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Arduino.h>
#include <stdint.h>

#include "../../sample_t.h"

#if 0
Expand Down
1 change: 0 additions & 1 deletion main/src/display/staffsymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdint.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>

#include "../../config.h"
#include "display.h"

Expand Down
2 changes: 1 addition & 1 deletion main/src/microphone/microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct amplitudeRange_t {
} amplitudeRange_t;

typedef struct microphone_t {
sampleCnt_t cnt;
sample_cnt_t cnt;
amplitudeRange_t range;
sample_t * samples;
uint8_t analogPort;
Expand Down
1 change: 0 additions & 1 deletion main/src/microphone/microphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Arduino.h>
#include <stdint.h>

#include "../../sample_t.h"

void microphone_begin(uint8_t const port); // 0 for A0
Expand Down
1 change: 0 additions & 1 deletion main/src/midi/midifile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Arduino.h>
#include <stdint.h>

#include "../../segment_t.h"

int_least8_t midifile_begin(uint_least8_t const cs);
Expand Down
4 changes: 2 additions & 2 deletions main/src/pitch/frequency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef enum state_t {
STATE_FIND_SECOND_PEAK,
} state_t;

typedef sampleCnt_t samplesLag_t;
typedef sample_cnt_t samplesLag_t;
typedef int32_t autoCorr_t;

// calculate auto correlation for "lag"
Expand All @@ -51,7 +51,7 @@ _auto_corr(samples_t const samples, // pointer to signed 8-bit data samples

autoCorr_t ac = 0;

for (sampleCnt_t ii = 0; ii < CONFIG_MIDIMIKE_WINDOW_SIZE - lag; ii++) {
for (sample_cnt_t ii = 0; ii < CONFIG_MIDIMIKE_WINDOW_SIZE - lag; ii++) {
ac += ((int16_t)samples[ii] * samples[ii + lag]);
}
return ac;
Expand Down
1 change: 0 additions & 1 deletion main/src/pitch/frequency.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Arduino.h>
#include <stdint.h>

#include "../../sample_t.h"

frequency_t frequency_calculate(samples_t const samples);
63 changes: 28 additions & 35 deletions main/src/sdcard/sddir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,42 @@
#include "../../config.h"
#include "sddir.h"

namespace {
// maximum level of sub directories to search
#define DIRLEVEL_MAX 2
// maximum level of sub directories to search
#define DIRLEVEL_MAX (2)

sddir_callback_t _callback = NULL;
sddir_callback_t _callback = NULL;

static uint_least8_t
_walkDirectory(File &dir, // directory to start
uint_least8_t const lvl) // current level
{
File f;
uint_least8_t err = 0;
static char instrumentName[8 + 1 + 3 + 1];

// Recursive search for files

uint_least8_t
_walkDirectory( File &dir, // directory to start
uint_least8_t const lvl ) // current level
{
File f;
uint_least8_t err = 0;
static char instrumentName[8 + 1 + 3 + 1];

while ( (f = dir.openNextFile()) && !err && _callback ) {
while ((f = dir.openNextFile()) && !err && _callback) {

if ( f.isDirectory() ) {
if ( lvl < DIRLEVEL_MAX ) {
if ( lvl == 0 ) {
strcpy( instrumentName, f.name() );
}
err = _walkDirectory( f, lvl + 1 ); // recursive function call
} else {
if (f.isDirectory()) {
if (lvl < DIRLEVEL_MAX) {
if (lvl == 0) {
strcpy(instrumentName, f.name());
}
err = _walkDirectory(f, lvl + 1); // recursive function call
} else {
err = _callback( f, instrumentName ); // call back registered function
}
f.close();
} else {
err = _callback(f, instrumentName); // call back registered function
}
return err;
f.close();
}
return err;
}

} // name space


// For each file in Directory
// for each file in Directory

uint_least8_t // returns 0 if successful
SdDir::forEachFile( char const * const dirName, // directory (and its sub directories) to search
sddir_callback_t cb ) // function to call for each file found
uint_least8_t // returns 0 if successful
sddir_for_each_file(char const * const dirName, // directory (and its sub directories) to search
sddir_callback_t cb) // function to call for each file found
{
_callback = cb;

Expand All @@ -85,10 +78,10 @@ SdDir::forEachFile( char const * const dirName, // directory (and its sub direc
}

uint_least8_t
SdDir::begin( uint_least8_t const cs )
sddir_init(uint_least8_t const cs_pin)
{
if ( SD.begin( cs ) == false ) { // declared in SD.cpp
if (SD.begin(cs_pin) == false) {
return 1;
}
return 0;
}
}
12 changes: 2 additions & 10 deletions main/src/sdcard/sddir.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
#include <Arduino.h>
#include <stdint.h>

// call back function definition
typedef uint_least8_t (* sddir_callback_t)(File & f, char * instrument);

namespace SdDir {

uint_least8_t
begin( uint_least8_t const cs ); // SD card CS pin

uint_least8_t // returns 0 if successful
forEachFile( char const * const dirName, // directory (and its sub directories) to search
sddir_callback_t cb); // function to call for each file found
};
uint_least8_t init(uint_least8_t const cs_pin);
uint_least8_t for_each_file(char const * const dirName, sddir_callback_t cb);
Loading