Fixed file not closing after require(). Changed seek() to use seconds.

This commit is contained in:
rude
2009-08-24 19:57:08 +02:00
parent de4d9bf2d4
commit 92c678ce02
10 changed files with 50 additions and 21 deletions
+2 -2
View File
@@ -92,10 +92,10 @@ namespace sound
/**
* Seeks to the specified position, if possible.
* @param ms The position in the stream in milliseconds.
* @param s The position in the stream in seconds.
* @return True if success, false on fail/unsupported.
**/
virtual bool seek(int ms) = 0;
virtual bool seek(float s) = 0;
/**
* Rewinds the stream to the start.
+2 -2
View File
@@ -73,9 +73,9 @@ namespace lullaby
return bufferSize;
}
bool FLACDecoder::seek(int ms)
bool FLACDecoder::seek(float s)
{
return seek_absolute(ms);
return seek_absolute((int)(s*1000.0f));
}
bool FLACDecoder::rewind()
+1 -1
View File
@@ -46,7 +46,7 @@ namespace lullaby
static bool accepts(const std::string &ext);
love::sound::Decoder *clone();
int decode();
bool seek(int ms);
bool seek(float s);
bool rewind();
bool isSeekable();
int getChannels() const;
+2 -2
View File
@@ -85,9 +85,9 @@ namespace lullaby
return r;
}
bool ModPlugDecoder::seek(int ms)
bool ModPlugDecoder::seek(float s)
{
ModPlug_Seek(plug, ms);
ModPlug_Seek(plug, (int)(s*1000.0f));
return true;
}
+1 -1
View File
@@ -50,7 +50,7 @@ namespace lullaby
love::sound::Decoder * clone();
int decode();
bool seek(int ms);
bool seek(float s);
bool rewind();
bool isSeekable();
int getChannels() const;
+38 -8
View File
@@ -58,9 +58,20 @@ namespace lullaby
if (ret != MPG123_OK)
throw love::Exception("Could not set output format.");
size_t numbytes = 0;
ret = mpg123_decode(handle, (unsigned char*) data->getData(), data->getSize(), NULL, 0, &numbytes);
if (ret != MPG123_DONE && ret != MPG123_NEW_FORMAT)
throw love::Exception("Could not decode feed.");
ret = mpg123_feed(handle, (unsigned char*)data->getData(), data->getSize());
if(ret == MPG123_NEW_FORMAT)
{
long rate;
int channels;
int encoding;
mpg123_getformat(handle, &rate, &channels, &encoding);
}
else if(ret != 0)
{
throw love::Exception(mpg123_strerror(handle));
}
}
Mpg123Decoder::~Mpg123Decoder()
@@ -98,9 +109,26 @@ namespace lullaby
int Mpg123Decoder::decode()
{
size_t numbytes;
int r = mpg123_read(handle, (unsigned char*) buffer, bufferSize, &numbytes);
for(int i = 0; i < 2; ++i)
{
int r = mpg123_read(handle, (unsigned char*) buffer, bufferSize, &numbytes);
switch(r)
{
case MPG123_NEW_FORMAT:
continue;
case MPG123_DONE:
eof = false;
case MPG123_OK:
default:
return numbytes;
}
}
// If we're done, then EOF. If some error occurred, pretend we EOF'd.
/*
if (r == MPG123_DONE || r != MPG123_OK)
{
if ( r == MPG123_NEED_MORE )
@@ -114,19 +142,21 @@ namespace lullaby
numbytes = 0;
}
}
*/
return numbytes;
}
bool Mpg123Decoder::seek(int ms)
bool Mpg123Decoder::seek(float s)
{
mpg123_seek(handle, ms, SEEK_SET);
return true;
off_t r = mpg123_seek(handle, (off_t)(s*1000.0f), SEEK_SET);
return (r >= 0);
}
bool Mpg123Decoder::rewind()
{
if(mpg123_seek(handle, 0, SEEK_SET) < 0)
if(mpg123_seek(handle, 0, SEEK_SET) >= 0)
return false;
return true;
}
+1 -1
View File
@@ -52,7 +52,7 @@ namespace lullaby
love::sound::Decoder * clone();
int decode();
bool seek(int ms);
bool seek(float s);
bool rewind();
bool isSeekable();
int getChannels() const;
+2 -2
View File
@@ -218,10 +218,10 @@ namespace lullaby
return size;
}
bool VorbisDecoder::seek(int ms)
bool VorbisDecoder::seek(float s)
{
eof = false;
int result = ov_time_seek(&handle, ms / 100);
int result = ov_time_seek(&handle, (int)(s*1000.0f) / 100);
if(result == 0)
return true;
+1 -1
View File
@@ -61,7 +61,7 @@ namespace lullaby
love::sound::Decoder * clone();
int decode();
bool seek(int ms);
bool seek(float s);
bool rewind();
bool isSeekable();
int getChannels() const;