]> git.tdb.fi Git - libs/al.git/blobdiff - source/mp3decoder.cpp
Implement seeking in sound decoders
[libs/al.git] / source / mp3decoder.cpp
index ec08be7f9a5969bfe2ee67ad81f4a1eb5adf6bbd..34604e320339dc5bdb0dcae98125e78d160ceee1 100644 (file)
@@ -87,9 +87,32 @@ bool Mp3Decoder::detect(const string &sig)
        return !sig.compare(0, sizeof(id3_sig), id3_sig);
 }
 
-void Mp3Decoder::rewind()
+void Mp3Decoder::seek(unsigned pos)
 {
+       unsigned unit = get_unit_size(format);
+       pos /= unit;
+
        in.seek(0, IO::S_BEG);
+       mad_stream_buffer(&priv->stream, 0, 0);
+       fill_input();
+       if(!pos)
+       {
+               priv->synth.pcm.length = 0;
+               return;
+       }
+
+       while(decode(false))
+       {
+               unsigned frame_len = 32*MAD_NSBSAMPLES(&priv->frame.header);
+               if(frame_len>pos)
+               {
+                       decode(true);
+                       read_pos = pos;
+                       break;
+               }
+
+               pos -= frame_len;
+       }
 }
 
 unsigned Mp3Decoder::read(char *buf, unsigned len)