]> camargo.eng.br - camargo/neiasound.git/blob - src/wav/nwavestream.cpp
1ac3145f5106153338afb50d75cc75136aa118de
[camargo/neiasound.git] / src / wav / nwavestream.cpp
1 // Copyright (C) 2015 Lucas Pires Camargo
2 // 
3 // This file is part of neiasound - Qt-style OpenAL wrapper for games.
4 // 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 // 
9 // 1. Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // 
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.
15 // 
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 #include "nwavestream.h"
27 #include "../nSoundBag.h"
28 #include <QIODevice>
29
30
31 nWaveStream::nWaveStream(QIODevice *device, nSoundFormat format, int frequency, int channels, QObject *parent)
32     : nSoundStream(parent),
33       _device(device),
34       _format(format),
35       _frequency(frequency),
36       _channels(channels)
37 {
38
39     if(format == SF_WAVE_HEADER)
40     {
41
42         unsigned char header[44];
43
44         device->read(reinterpret_cast<char*>(header), 44);
45         _channels = header[22];
46         _frequency = header[24] + (((int)header[25])<<8) + (((int)header[26])<<16) + (((int)header[27])<<24);
47         _format = (_channels == 1?
48                        (header[34]==16? SF_16BIT_MONO : SF_8BIT_MONO):
49                        (header[34]==16? SF_16BIT_STEREO : SF_8BIT_STEREO)
50                        );
51         int chunkSize = header[40] + (((int)header[41])<<8) + (((int)header[42])<<16) + (((int)header[43])<<24);
52         _totalFrames = chunkSize / nSoundFormat_getFramesize(_format);
53     }
54     else
55     {
56         if(channels < 0) channels = (format == SF_8BIT_STEREO || format  == SF_16BIT_STEREO)? 2 : 1;
57     }
58 }
59
60 nSoundBag *nWaveStream::createSoundBag(QObject *parent)
61 {
62     nSoundBag * bag = new nSoundBag( _format, _totalFrames, _frequency );
63     read(bag->m_data, _totalFrames);
64     return bag;
65 }
66
67
68 void nWaveStream::rewind()
69 {
70     _device->reset();
71 }
72
73 quint64 nWaveStream::read(void *data, unsigned long frames)
74 {
75     return _device->read( (char *) data, nSoundFormat_getFramesize(_format) * frames ) / nSoundFormat_getFramesize(_format);
76 }