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 #include "nwavestream.h"
27 #include "../nSoundBag.h"
31 nWaveStream::nWaveStream(QIODevice *device, nSoundFormat format, int frequency, int channels, QObject *parent)
32 : nSoundStream(parent),
35 _frequency(frequency),
39 if(format == SF_WAVE_HEADER)
42 unsigned char header[44];
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)
51 int chunkSize = header[40] + (((int)header[41])<<8) + (((int)header[42])<<16) + (((int)header[43])<<24);
52 _totalFrames = chunkSize / nSoundFormat_getFramesize(_format);
56 if(channels < 0) channels = (format == SF_8BIT_STEREO || format == SF_16BIT_STEREO)? 2 : 1;
60 nSoundBag *nWaveStream::createSoundBag(QObject *parent)
62 nSoundBag * bag = new nSoundBag( _format, _totalFrames, _frequency );
63 read(bag->m_data, _totalFrames);
68 void nWaveStream::rewind()
73 quint64 nWaveStream::read(void *data, unsigned long frames)
75 return _device->read( (char *) data, nSoundFormat_getFramesize(_format) * frames ) / nSoundFormat_getFramesize(_format);