play small mp3s with id tags

This commit is contained in:
Adam D. Ruppe 2020-12-31 14:11:13 -05:00
parent 28fb1cf3fc
commit b82a85493c
2 changed files with 18 additions and 8 deletions

8
mp3.d
View File

@ -301,8 +301,8 @@ private:
removeBytes(left); removeBytes(left);
sz -= left; sz -= left;
} }
if (eofhit) { curFrameIsOk = false; return false; } if (!eofhit)
continue; continue;
} }
} }
} else { } else {
@ -316,8 +316,8 @@ private:
removeBytes(left); removeBytes(left);
sz -= left; sz -= left;
} }
if (eofhit) { curFrameIsOk = false; return false; } if (!eofhit)
continue; continue;
} }
} }

View File

@ -256,6 +256,8 @@ private class SampleControlFlags : SampleController {
struct AudioOutputThread { struct AudioOutputThread {
@disable this(); @disable this();
@disable new(size_t); // gdc9 requires the arg fyi
/++ /++
Pass `true` to enable the audio thread. Otherwise, it will Pass `true` to enable the audio thread. Otherwise, it will
just live as a dummy mock object that you should not actually just live as a dummy mock object that you should not actually
@ -324,6 +326,8 @@ struct AudioOutputThread {
else static assert(0); else static assert(0);
} }
// since these are templates, the opDispatch won't trigger them, so I have to do it differently.
// the dummysample is good anyway.
SampleController playEmulatedOpl3Midi()(string filename) { SampleController playEmulatedOpl3Midi()(string filename) {
if(impl) if(impl)
return impl.playEmulatedOpl3Midi(filename); return impl.playEmulatedOpl3Midi(filename);
@ -709,7 +713,7 @@ final class AudioPcmOutThreadImplementation : Thread {
SampleController playMp3()(string filename) { SampleController playMp3()(string filename) {
import std.stdio; import std.stdio;
auto fi = new File(filename); // just let the GC close it... otherwise random segfaults happen... blargh auto fi = new File(filename); // just let the GC close it... otherwise random segfaults happen... blargh
scope auto reader = delegate(void[] buf) { auto reader = delegate(void[] buf) {
return cast(int) fi.rawRead(buf[]).length; return cast(int) fi.rawRead(buf[]).length;
}; };
@ -740,7 +744,7 @@ final class AudioPcmOutThreadImplementation : Thread {
auto mp3 = new MP3Decoder(reader); auto mp3 = new MP3Decoder(reader);
if(!mp3.valid) if(!mp3.valid)
throw new Exception("no file"); throw new Exception("file not valid");
auto scf = new SampleControlFlags; auto scf = new SampleControlFlags;
@ -1511,8 +1515,14 @@ struct AudioOutput {
short[BUFFER_SIZE_SHORT] buffer; short[BUFFER_SIZE_SHORT] buffer;
while(playing) { while(playing) {
auto err = snd_pcm_wait(handle, 500); auto err = snd_pcm_wait(handle, 500);
if(err < 0) if(err < 0) {
throw new AlsaException("uh oh", err); // see: https://stackoverflow.com/a/59400592/1457000
err = snd_pcm_recover(handle, err, 0);
if(err)
throw new AlsaException("pcm recover failed after pcm_wait did ", err);
//throw new AlsaException("uh oh", err);
continue;
}
// err == 0 means timeout // err == 0 means timeout
// err == 1 means ready // err == 1 means ready