1 // Copyright (C) 2015 Lucas Pires Camargo
3 // This file is part of neiasound - Qt-style OpenAL wrapper for games.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // 1. Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS
17 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 // NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // Copyright (C) 2015 Lucas Pires Camargo
28 // This file is part of neiasound - Qt-style OpenAL wrapper for games.
30 // Redistribution and use in source and binary forms, with or without
31 // modification, are permitted provided that the following conditions are
34 // 1. Redistributions of source code must retain the above copyright notice,
35 // this list of conditions and the following disclaimer.
37 // 2. Redistributions in binary form must reproduce the above copyright notice,
38 // this list of conditions and the following disclaimer in the documentation
39 // and/or other materials provided with the distribution.
41 // THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS
42 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
44 // NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
45 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
50 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include "nvorbisstream.h"
54 #include "../nSoundBag.h"
57 #define STB_VORBIS_MAX_CHANNELS 2
58 #pragma GCC diagnostic push
59 #pragma GCC diagnostic ignored "-Wsign-compare"
60 #pragma GCC diagnostic ignored "-Wtype-limits"
61 #pragma GCC diagnostic ignored "-Wparentheses"
62 #pragma GCC diagnostic ignored "-Wunused-parameter"
63 #pragma GCC diagnostic ignored "-Wunused-value"
64 #pragma GCC diagnostic ignored "-Wunused-variable"
65 #pragma GCC diagnostic ignored "-Wunused-function"
66 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
67 #include "stb_vorbis.c"
68 #pragma GCC diagnostic pop
71 nVorbisStream::nVorbisStream(QIODevice * dev, QObject *parent) : nSoundStream(parent),
76 _format(SF_16BIT_STEREO),
82 dev->open(QIODevice::ReadOnly);
84 _qtBuf = dev->readAll();
90 _bufSize = _qtBuf.size();
94 int err = VORBIS__no_error;
95 _vorbis = stb_vorbis_open_memory((unsigned char*)_buf, _bufSize, &err, 0 );
96 if(!_vorbis || err != VORBIS__no_error)
98 qDebug("[nVorbisStream] Error initializing vorbis stream");
101 //stb_vorbis_info info = stb_vorbis_get_info(_vorbis);
103 _totalFrames = stb_vorbis_stream_length_in_samples(_vorbis);
104 _channels = _vorbis->channels;
105 _frequency = _vorbis->sample_rate;
107 switch (channels()) {
109 _format = SF_16BIT_MONO;
112 _format = SF_16BIT_STEREO;
115 _format = SF_UNDEFINED;
121 qDebug("[nVorbisStream] Error reading QIODevice");
126 nVorbisStream::~nVorbisStream()
128 stb_vorbis_close(_vorbis);
132 void nVorbisStream::rewind()
134 if(_vorbis) stb_vorbis_seek_start(_vorbis);
137 quint64 nVorbisStream::read(void *data, unsigned long frames)
139 if(!frames) return 0;
140 return stb_vorbis_get_samples_short_interleaved(_vorbis, _channels, (short*) data, frames * _channels );