+SoundBuffer *load_ogg_from_file(const std::string &path)
+{
+ OggVorbis_File oggFile;
+
+ // Try opening the given file.
+ // This requires libvorbis >= 1.3.2, as
+ // previous versions expect a non-const char *
+ if (ov_fopen(path.c_str(), &oggFile) != 0) {
+ infostream << "Audio: Error opening " << path
+ << " for decoding" << std::endl;
+ return NULL;
+ }
+
+ return load_opened_ogg_file(&oggFile, path);
+}
+
+struct BufferSource {
+ const char *buf;
+ size_t cur_offset;
+ size_t len;
+};
+
+size_t buffer_sound_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
+{
+ BufferSource *s = (BufferSource *)datasource;
+ size_t copied_size = MYMIN(s->len - s->cur_offset, size);
+ memcpy(ptr, s->buf + s->cur_offset, copied_size);
+ s->cur_offset += copied_size;
+ return copied_size;
+}
+
+int buffer_sound_seek_func(void *datasource, ogg_int64_t offset, int whence)
+{
+ BufferSource *s = (BufferSource *)datasource;
+ if (whence == SEEK_SET) {
+ if (offset < 0 || (size_t)MYMAX(offset, 0) >= s->len) {
+ // offset out of bounds
+ return -1;
+ }
+ s->cur_offset = offset;
+ return 0;
+ } else if (whence == SEEK_CUR) {
+ if ((size_t)MYMIN(-offset, 0) > s->cur_offset
+ || s->cur_offset + offset > s->len) {
+ // offset out of bounds
+ return -1;
+ }
+ s->cur_offset += offset;
+ return 0;
+ }
+ // invalid whence param (SEEK_END doesn't have to be supported)
+ return -1;
+}
+
+long BufferSourceell_func(void *datasource)
+{
+ BufferSource *s = (BufferSource *)datasource;
+ return s->cur_offset;
+}
+
+static ov_callbacks g_buffer_ov_callbacks = {
+ &buffer_sound_read_func,
+ &buffer_sound_seek_func,
+ NULL,
+ &BufferSourceell_func
+};
+
+SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_for_log)
+{
+ OggVorbis_File oggFile;
+
+ BufferSource s;
+ s.buf = buf.c_str();
+ s.cur_offset = 0;
+ s.len = buf.size();
+
+ if (ov_open_callbacks(&s, &oggFile, NULL, 0, g_buffer_ov_callbacks) != 0) {
+ infostream << "Audio: Error opening " << id_for_log
+ << " for decoding" << std::endl;
+ return NULL;
+ }
+
+ return load_opened_ogg_file(&oggFile, id_for_log);
+}
+