Big Mouth Billy Bass automation library
Diff: player.hpp
- Revision:
- 4:f009306756b3
- Parent:
- 3:6c91a6232c4a
- Child:
- 6:ea8136eb6976
--- a/player.hpp Tue Jun 18 13:11:07 2013 +0000 +++ b/player.hpp Tue Jun 18 14:10:34 2013 +0000 @@ -47,7 +47,7 @@ float timeInSong; Ticker sampleTicker; Song *song; - Song::actions_t::iterator nextAction; + Action *nextAction; unsigned actionsDone; SongPlayer() : playing(0) @@ -68,26 +68,25 @@ } bool startSong(Song *_song) { - if (song) delete song; song = _song; - nextAction = song->getActions().begin(); + nextAction = song->getActions(); timeInSong = 0.0; actionsDone = 0; fprintf(stderr, "starting %s: ", song->getSampleFileName()); if (fp) fclose(fp); fp = fopen(song->getSampleFileName(), "rb"); + if (!fp) goto on_error; fprintf(stderr, "opened, "); - if (!fp) return false; + if (fseek(fp, 0, SEEK_END)) goto on_error; fprintf(stderr, "seekend, "); - if (fseek(fp, 0, SEEK_END)) return false; long fileSize = ftell(fp); fprintf(stderr, "size=%d, ", fileSize); - if (fileSize < 0) return false; + if (fileSize < 0) goto on_error; - if (fseek(fp, 0, SEEK_SET)) return false; + if (fseek(fp, 0, SEEK_SET)) goto on_error; fprintf(stderr, "rewound, "); chunksRemaining = nChunks = fileSize / BUFFER_SIZE; @@ -96,11 +95,16 @@ fprintf(stderr, "chunks=%d expected=%f seconds\r\n", nChunks, nChunks * SECONDS_PER_CHUNK); if (!loadNextChunk()) { fprintf(stderr, "first chunk empty!\r\n"); - return false; + goto on_error; } sampleTicker.attach_us(this, &SongPlayer::playNextSample, SAMPLE_PERIOD_USEC); return true; + +on_error: + if (fp) fclose(fp); + fp = 0; + return false; } // swap loading/playing buffers; @@ -133,7 +137,7 @@ // look at loading buffer; load only if necessary. bool loadIfNecessary() { if (loading->isDone()) { - fprintf(stderr, "*"); + fprintf(stderr, "*"); timeInSong += SECONDS_PER_CHUNK; return loadNextChunk(); } else { @@ -143,10 +147,10 @@ void playEntireSong(Song *_song) { if (!startSong(_song)) return; - + Action* lastAction = song->getActions() + song->getNumActions(); while (!isDone()) { - while (nextAction != song->getActions().end() && nextAction->actIfPast(timeInSong)) { - fprintf(stderr, "."); + while (nextAction < lastAction && nextAction->actIfPast(timeInSong)) { + fprintf(stderr, "."); actionsDone++; nextAction++; }