1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include "stdafx.h"
#include "Mp3Sound.h"
#include "Exception.h"
#include "Core/Convert.h"
#include "Utils/Memory.h"
#include <limits>
namespace sound {
/**
* Allocated in a non-moving pool so that we can pass the handle to mpg123.
*/
struct Mp3Stream {
IStream *src;
Bool seekable;
};
/**
* Type description for Mp3Stream above.
*/
static storm::GcType mp3StreamType = {
GcType::tFixed,
null, // type
null, // finalizer
sizeof(Mp3Stream), // stride
1, // # of offsets
{ OFFSET_OF(Mp3Stream, src) }, // Offset to 'stream'.
};
Mp3Sound::Mp3Sound(IStream *src, Bool seekable) : rate(0), ch(0), h(null), stream(null), atEnd(false) {
// TODO: Pick dithering decoder?
h = mpg123_new(NULL, NULL);
int flags = MPG123_FORCE_FLOAT;
// Note: we can use MPG123_PICTURE to load ID3v2 images.
if (seekable)
flags |= MPG123_FORCE_SEEKABLE;
else
flags |= MPG123_NO_PEEK_END;
mpg123_param(h, MPG123_FLAGS, flags, 0);
stream = (Mp3Stream *)runtime::allocStaticRaw(engine(), &mp3StreamType);
stream->src = src;
stream->seekable = seekable;
mpg123_replace_reader_handle(h, &Mp3Sound::read, &Mp3Sound::seek, &Mp3Sound::cleanup);
int ok = mpg123_open_handle(h, stream);
long rate = 0;
int channels, encoding = 0;
if (ok == MPG123_OK) {
ok = mpg123_getformat(h, &rate, &channels, &encoding);
}
if (ok == MPG123_OK) {
// It seems mpg123 could indicate 'stereo and mono' using 'channels == 3'.
if (channels > 2)
channels = 2;
this->rate = rate;
this->ch = channels;
if (encoding != MPG123_ENC_FLOAT_32) {
mpg123_delete(h);
h = null;
throw new (this) SoundOpenError(S("Expected float output from mpg123."));
}
} else {
Str *msg = new (this) Str(toWChar(engine(), mpg123_strerror(h)));
mpg123_delete(h);
h = null;
throw new (this) SoundOpenError(msg);
}
}
Mp3Sound::~Mp3Sound() {
// Do not try to close the stream. That might already have been done.
stream = null;
close();
}
void Mp3Sound::close() {
// Close MP3 decoder.
if (h) {
mpg123_delete(h);
h = null;
}
if (stream && stream->src) {
stream->src->close();
stream->src = null;
stream = null;
}
}
Word Mp3Sound::tell() {
if (stream && stream->seekable) {
return Word(mpg123_tell(h));
}
return 0;
}
Bool Mp3Sound::seek(Word to) {
if (stream && stream->seekable) {
atEnd = false;
return mpg123_seek(h, off_t(to), SEEK_SET) >= 0;
}
return false;
}
Word Mp3Sound::length() {
if (stream && stream->seekable) {
off_t l = mpg123_length(h);
if (l == MPG123_ERR) {
// Try calling mpg123_scan to find the length.
mpg123_scan(h);
l = mpg123_length(h);
}
if (l == MPG123_ERR)
return 0;
return Word(l);
}
return 0;
}
Nat Mp3Sound::sampleFreq() const {
return rate;
}
Nat Mp3Sound::channels() const {
return ch;
}
Buffer Mp3Sound::read(Buffer to) {
if (atEnd)
return to;
Nat free = to.count() - to.filled();
size_t filled = 0;
int ok = mpg123_read(h, (byte *)(to.dataPtr() + to.filled()), free * sizeof(float), &filled);
if (ok != MPG123_OK) {
switch (ok) {
case MPG123_NEW_FORMAT:
WARNING(L"Multi-format files not supported.");
break;
case MPG123_DONE:
break;
default:
WARNING(mpg123_strerror(h));
break;
}
atEnd = true;
}
if (filled == 0)
atEnd = true;
to.filled(to.filled() + Nat(filled/sizeof(float)));
return to;
}
Bool Mp3Sound::more() {
return !atEnd;
}
ssize_t Mp3Sound::read(void *handle, void *to, size_t count) {
Mp3Stream *stream = (Mp3Stream *)handle;
storm::Buffer b = stream->src->read(Nat(min(size_t(std::numeric_limits<Nat>::max()), count)));
memcpy(to, b.dataPtr(), b.filled());
return b.filled();
}
off_t Mp3Sound::seek(void *handle, off_t offset, int mode) {
Mp3Stream *stream = (Mp3Stream *)handle;
if (!stream->seekable)
return -1;
// We know this is possible since we checked 'seekable'.
RIStream *src = (RIStream *)stream->src;
switch (mode) {
case SEEK_SET:
src->seek(Word(offset));
break;
case SEEK_CUR:
src->seek(Word(src->tell() + offset));
break;
case SEEK_END:
src->seek(Word(src->length() + offset));
break;
default:
return -1;
}
return off_t(src->tell());
}
void Mp3Sound::cleanup(void *handle) {
Mp3Stream *stream = (Mp3Stream *)handle;
if (stream->src)
stream->src->close();
}
Mp3Sound *openMp3(RIStream *src) {
return new (src) Mp3Sound(src, true);
}
Mp3Sound *openMp3Stream(IStream *src) {
return new (src) Mp3Sound(src, false);
}
}
|