Initial commit
[camargo/neiasound.git] / src / wav / nwavestream.cpp
1 #include "nwavestream.h"
2 #include "../nSoundBag.h"
3 #include <QIODevice>
4
5
6 nWaveStream::nWaveStream(QIODevice *device, nSoundFormat format, int frequency, int channels, QObject *parent)
7     : nSoundStream(parent),
8       _device(device),
9       _format(format),
10       _frequency(frequency),
11       _channels(channels)
12 {
13
14     if(format == SF_WAVE_HEADER)
15     {
16
17         unsigned char header[44];
18
19         device->read(reinterpret_cast<char*>(header), 44);
20         _channels = header[22];
21         _frequency = header[24] + (((int)header[25])<<8) + (((int)header[26])<<16) + (((int)header[27])<<24);
22         _format = (_channels == 1?
23                        (header[34]==16? SF_16BIT_MONO : SF_8BIT_MONO):
24                        (header[34]==16? SF_16BIT_STEREO : SF_8BIT_STEREO)
25                        );
26         int chunkSize = header[40] + (((int)header[41])<<8) + (((int)header[42])<<16) + (((int)header[43])<<24);
27         _totalFrames = chunkSize / nSoundFormat_getFramesize(_format);
28     }
29     else
30     {
31         if(channels < 0) channels = (format == SF_8BIT_STEREO || format  == SF_16BIT_STEREO)? 2 : 1;
32     }
33 }
34
35 nSoundBag *nWaveStream::createSoundBag(QObject *parent)
36 {
37     nSoundBag * bag = new nSoundBag( _format, _totalFrames, _frequency );
38     read(bag->m_data, _totalFrames);
39     return bag;
40 }
41
42
43 void nWaveStream::rewind()
44 {
45     _device->reset();
46 }
47
48 quint64 nWaveStream::read(void *data, unsigned long frames)
49 {
50     return _device->read( (char *) data, nSoundFormat_getFramesize(_format) * frames ) / nSoundFormat_getFramesize(_format);
51 }