3 This file is part of libmspal
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/core/except.h>
24 Sound::Sound(const std::string &fn):
39 void Sound::open(const string &fn)
42 throw InvalidState("Sound has already been opened");
43 if(ov_fopen(const_cast<char *>(fn.c_str()), &ovfile)<0)
44 throw Exception("Could not open ogg vorbis file "+fn);
49 vorbis_info *info=ov_info(&ovfile, -1);
51 switch(info->channels)
53 case 1: format=MONO16; break;
54 case 2: format=STEREO16; break;
55 default: throw Exception("Unsupported number of channels");
59 void Sound::load_data()
62 throw InvalidState("Data has already been loaded");
64 size=ov_pcm_total(&ovfile, 0)*4;
65 char *dptr=new char[size];
67 while(unsigned len=read(dptr+pos, size-pos))
73 void Sound::load(const string &fn)
91 ov_pcm_seek(&ovfile, 0);
94 unsigned Sound::read(char *buf, unsigned len)
98 len=min(len, size-read_pos);
99 memcpy(buf, data+read_pos, len);
103 else if(ovfile.datasource)
106 int res=ov_read(&ovfile, buf, len, 0, 2, 1, §ion);
108 throw Exception("Error reading ogg vorbis file");
114 throw InvalidState("No data available");
117 const char *Sound::get_data() const
120 throw InvalidState("Data has not been loaded");