Big Mouth Billy Bass automation library

Dependents:   BillyBass_with_SD

Revision:
3:6c91a6232c4a
Parent:
2:eaba75af0f0d
Child:
4:f009306756b3
--- a/player.hpp	Tue Jun 18 06:12:48 2013 +0000
+++ b/player.hpp	Tue Jun 18 13:11:07 2013 +0000
@@ -3,6 +3,8 @@
 
 #include "billybass.hpp"
 
+extern AnalogOut speaker;
+
 class SongPlayer;
 
 struct SampleBuffer {
@@ -54,8 +56,7 @@
         , nChunks(0)
         , chunksRemaining(0)
         , song(0)
-        , actionsDone(0)
-     {
+        , actionsDone(0) {
     }
 
     // interrupt handler
@@ -63,7 +64,7 @@
         if (playing->samplesRemaining == 0)
             swapBuffers();
         // NOTE bias of 0xC000 requires normalizing to 75% of full scale
-        speaker.write_u16(static_cast<uint16_t>(playing->getNextSample() + 0x8000) / 2);
+        speaker.write_u16(static_cast<uint16_t>(playing->getNextSample() + ANALOG_OUTPUT_BIAS));
     }
 
     bool startSong(Song *_song) {
@@ -73,29 +74,30 @@
         timeInSong = 0.0;
         actionsDone = 0;
 
-        pc.printf("starting %s: ", song->getSampleFileName());
+        fprintf(stderr, "starting %s: ", song->getSampleFileName());
         if (fp) fclose(fp);
         fp = fopen(song->getSampleFileName(), "rb");
-        pc.printf("opened, ");
+        fprintf(stderr, "opened, ");
         if (!fp) return false;
 
-        pc.printf("seekend, ");
+        fprintf(stderr, "seekend, ");
         if (fseek(fp, 0, SEEK_END)) return false;
 
         long fileSize = ftell(fp);
-        pc.printf("size=%d, ", fileSize);
+        fprintf(stderr, "size=%d, ", fileSize);
         if (fileSize < 0) return false;
 
         if (fseek(fp, 0, SEEK_SET)) return false;
 
-        pc.printf("rewound, ");
+        fprintf(stderr, "rewound, ");
         chunksRemaining = nChunks = fileSize / BUFFER_SIZE;
         loading         = &buffer[0];
         playing         = &buffer[1];
-        pc.printf("chunks=%d expected=%f seconds\r\n", nChunks, nChunks * SECONDS_PER_CHUNK);
-        if (!loadNextChunk())
+        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;
-
+        }
 
         sampleTicker.attach_us(this, &SongPlayer::playNextSample, SAMPLE_PERIOD_USEC);
         return true;
@@ -118,8 +120,8 @@
     // to prepare for eventual swap.
     // returns true if more samples remain
     bool loadNextChunk() {
-        if (!chunksRemaining) return false;
-
+        if (isDone())
+            return false;
         bool notDone = loading->loadFrom(fp);
         return notDone;
     }
@@ -131,6 +133,7 @@
     // look at loading buffer; load only if necessary.
     bool loadIfNecessary() {
         if (loading->isDone()) {
+        fprintf(stderr, "*");
             timeInSong += SECONDS_PER_CHUNK;
             return loadNextChunk();
         } else {
@@ -143,12 +146,14 @@
 
         while (!isDone()) {
             while (nextAction != song->getActions().end() && nextAction->actIfPast(timeInSong)) {
+            fprintf(stderr, ".");
                 actionsDone++;
                 nextAction++;
             }
             loadIfNecessary();
         }
         sampleTicker.detach();
+        song->myFish()->relax();
     }
 };