]> camargo.eng.br - camargo/neiasound.git/blob - src/stb_vorbis/nvorbisstream.cpp
Initial commit
[camargo/neiasound.git] / src / stb_vorbis / nvorbisstream.cpp
1 #include "nvorbisstream.h"
2
3 #include <QIODevice>
4 #include "../nSoundBag.h"
5
6
7 #define STB_VORBIS_MAX_CHANNELS     2
8 #include "stb_vorbis.c"
9
10
11 nVorbisStream::nVorbisStream(QIODevice * dev, QObject *parent) : nSoundStream(parent),
12     _device(dev),
13     m_error(false),
14     _totalFrames(0),
15     _channels(0),
16     _frequency(0),
17     _format(SF_16BIT_STEREO)
18 {
19     _vorbis = 0;
20
21     if(!dev->isOpen())
22         dev->open(QIODevice::ReadOnly);
23
24     _qtBuf = dev->readAll();
25
26     dev->close();
27
28     if(_qtBuf.size())
29     {
30         _bufSize = _qtBuf.size();
31         _buf = _qtBuf.data();
32
33
34         int err = VORBIS__no_error;
35         _vorbis = stb_vorbis_open_memory((unsigned char*)_buf, _bufSize, &err,  0 );
36         if(!_vorbis || err != VORBIS__no_error)
37         {
38             qDebug(QStringLiteral("[nVorbisStream] Error initializing vorbis stream: %1").arg(err).toLocal8Bit());
39         }
40
41         stb_vorbis_info info = stb_vorbis_get_info(_vorbis);
42
43         _totalFrames = stb_vorbis_stream_length_in_samples(_vorbis);
44         _channels = _vorbis->channels;
45         _frequency = _vorbis->sample_rate;
46
47         switch (channels()) {
48         case 1:
49             _format = SF_16BIT_MONO;
50             break;
51         case 2:
52             _format = SF_16BIT_STEREO;
53             break;
54         default:
55             _format = SF_UNDEFINED;
56             break;
57         }
58     }
59     else
60     {
61         qDebug("[nVorbisStream] Error reading QIODevice");
62     }
63
64 }
65
66 nVorbisStream::~nVorbisStream()
67 {
68     stb_vorbis_close(_vorbis);
69     _qtBuf.clear();
70 }
71
72 void nVorbisStream::rewind()
73 {
74     if(_vorbis) stb_vorbis_seek_start(_vorbis);
75 }
76
77 quint64 nVorbisStream::read(void *data, unsigned long frames)
78 {
79     if(!frames) return 0;
80     return stb_vorbis_get_samples_short_interleaved(_vorbis, _channels, (short*) data, frames * _channels );
81
82 }
83