Big Mouth Billy Bass player that takes raw wavefiles and decision list text files from an SD card
Dependencies: SDFileSystem mbed BillyBass
song.hpp@5:5b846ef42702, 2013-06-15 (annotated)
- Committer:
- bikeNomad
- Date:
- Sat Jun 15 03:32:20 2013 +0000
- Revision:
- 5:5b846ef42702
separated into multiple files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bikeNomad | 5:5b846ef42702 | 1 | #ifndef __included_song_hpp |
bikeNomad | 5:5b846ef42702 | 2 | #define __included_song_hpp |
bikeNomad | 5:5b846ef42702 | 3 | |
bikeNomad | 5:5b846ef42702 | 4 | #include "billybass.hpp" |
bikeNomad | 5:5b846ef42702 | 5 | #include "action.hpp" |
bikeNomad | 5:5b846ef42702 | 6 | |
bikeNomad | 5:5b846ef42702 | 7 | struct Song |
bikeNomad | 5:5b846ef42702 | 8 | { |
bikeNomad | 5:5b846ef42702 | 9 | char basename[ MAX_BASENAME_LENGTH + 1 ]; |
bikeNomad | 5:5b846ef42702 | 10 | unsigned sequenceNumber; |
bikeNomad | 5:5b846ef42702 | 11 | unsigned whichFish; |
bikeNomad | 5:5b846ef42702 | 12 | std::list<Action> actions; |
bikeNomad | 5:5b846ef42702 | 13 | |
bikeNomad | 5:5b846ef42702 | 14 | Song() : sequenceNumber(0) |
bikeNomad | 5:5b846ef42702 | 15 | , whichFish(3) { |
bikeNomad | 5:5b846ef42702 | 16 | basename[0] = 0; |
bikeNomad | 5:5b846ef42702 | 17 | } |
bikeNomad | 5:5b846ef42702 | 18 | |
bikeNomad | 5:5b846ef42702 | 19 | bool readWaveFile(char const *_waveFileName) |
bikeNomad | 5:5b846ef42702 | 20 | { |
bikeNomad | 5:5b846ef42702 | 21 | if (!parseFilename(_waveFileName, sequenceNumber, whichFish, basename)) return false; |
bikeNomad | 5:5b846ef42702 | 22 | |
bikeNomad | 5:5b846ef42702 | 23 | char txtFileName[ FILENAME_MAX ]; |
bikeNomad | 5:5b846ef42702 | 24 | sprintf(txtFileName, "%u_%u_%s.txt", sequenceNumber, whichFish, basename); |
bikeNomad | 5:5b846ef42702 | 25 | FILE *txtfile = fopen(txtFileName, "rt"); |
bikeNomad | 5:5b846ef42702 | 26 | if (!txtfile) return false; // TODO |
bikeNomad | 5:5b846ef42702 | 27 | |
bikeNomad | 5:5b846ef42702 | 28 | // read actions from file |
bikeNomad | 5:5b846ef42702 | 29 | char actionLine[ MAX_ACTION_LINE_LENGTH + 1 ]; |
bikeNomad | 5:5b846ef42702 | 30 | while (fgets(actionLine, sizeof(actionLine), txtfile)) |
bikeNomad | 5:5b846ef42702 | 31 | { |
bikeNomad | 5:5b846ef42702 | 32 | Action action; |
bikeNomad | 5:5b846ef42702 | 33 | if (action.parseLine(actionLine)) |
bikeNomad | 5:5b846ef42702 | 34 | { |
bikeNomad | 5:5b846ef42702 | 35 | actions.push_back(action); |
bikeNomad | 5:5b846ef42702 | 36 | } |
bikeNomad | 5:5b846ef42702 | 37 | } |
bikeNomad | 5:5b846ef42702 | 38 | return true; |
bikeNomad | 5:5b846ef42702 | 39 | } |
bikeNomad | 5:5b846ef42702 | 40 | |
bikeNomad | 5:5b846ef42702 | 41 | static bool parseFilename(char const *_name, unsigned &_num1, unsigned &_num2, char *_basename) |
bikeNomad | 5:5b846ef42702 | 42 | { |
bikeNomad | 5:5b846ef42702 | 43 | char basename[ MAX_BASENAME_LENGTH + 1 ]; |
bikeNomad | 5:5b846ef42702 | 44 | unsigned num1, num2; |
bikeNomad | 5:5b846ef42702 | 45 | char extension[ 4 ]; |
bikeNomad | 5:5b846ef42702 | 46 | int nItems = sscanf(_name, BASS_DIRECTORY "/%u_%u_%s", &num1, &num2, basename); |
bikeNomad | 5:5b846ef42702 | 47 | if (nItems != 3) |
bikeNomad | 5:5b846ef42702 | 48 | { |
bikeNomad | 5:5b846ef42702 | 49 | return false; |
bikeNomad | 5:5b846ef42702 | 50 | } |
bikeNomad | 5:5b846ef42702 | 51 | char *p = strrchr(basename, '.'); |
bikeNomad | 5:5b846ef42702 | 52 | if (!p) |
bikeNomad | 5:5b846ef42702 | 53 | { |
bikeNomad | 5:5b846ef42702 | 54 | return false; |
bikeNomad | 5:5b846ef42702 | 55 | } |
bikeNomad | 5:5b846ef42702 | 56 | strcpy(extension, p+1); |
bikeNomad | 5:5b846ef42702 | 57 | *p = 0; |
bikeNomad | 5:5b846ef42702 | 58 | if (num2 > 2) |
bikeNomad | 5:5b846ef42702 | 59 | { |
bikeNomad | 5:5b846ef42702 | 60 | return false; |
bikeNomad | 5:5b846ef42702 | 61 | } |
bikeNomad | 5:5b846ef42702 | 62 | if (strcasecmp("raw", extension)) |
bikeNomad | 5:5b846ef42702 | 63 | { |
bikeNomad | 5:5b846ef42702 | 64 | return false; |
bikeNomad | 5:5b846ef42702 | 65 | } |
bikeNomad | 5:5b846ef42702 | 66 | _num1 = num1; |
bikeNomad | 5:5b846ef42702 | 67 | _num2 = num2; |
bikeNomad | 5:5b846ef42702 | 68 | strcpy(_basename, basename); |
bikeNomad | 5:5b846ef42702 | 69 | return true; |
bikeNomad | 5:5b846ef42702 | 70 | } |
bikeNomad | 5:5b846ef42702 | 71 | |
bikeNomad | 5:5b846ef42702 | 72 | // return true if filename is of proper format |
bikeNomad | 5:5b846ef42702 | 73 | // <num>_<num>_<name>.raw |
bikeNomad | 5:5b846ef42702 | 74 | // and there is a corresponding .txt file |
bikeNomad | 5:5b846ef42702 | 75 | static bool isValidWaveFileName(char const *_name) |
bikeNomad | 5:5b846ef42702 | 76 | { |
bikeNomad | 5:5b846ef42702 | 77 | unsigned int num1, num2; |
bikeNomad | 5:5b846ef42702 | 78 | char basename[ 31 ]; |
bikeNomad | 5:5b846ef42702 | 79 | return parseFilename(_name, num1, num2, basename); |
bikeNomad | 5:5b846ef42702 | 80 | } |
bikeNomad | 5:5b846ef42702 | 81 | }; |
bikeNomad | 5:5b846ef42702 | 82 | |
bikeNomad | 5:5b846ef42702 | 83 | #endif // __included_song_hpp |