1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef __GE_IGA_AUDIOBUFFER__
42 #define __GE_IGA_AUDIOBUFFER__
43 
44 #include <QFile>
45 #include "GEInterfaces.h"
46 
47 
48 namespace GE {
49 
50     class CAudioBufferPlayInstance;
51     class CAudioBuffer;         // forward declaration
52     typedef AUDIO_SAMPLE_TYPE(*SAMPLE_FUNCTION_TYPE)(CAudioBuffer *abuffer, int pos, int channel);
53 
54     class CAudioBuffer {            // container for a sound
55     public:
56         CAudioBuffer();
57         virtual ~CAudioBuffer();
58 
59         static CAudioBuffer* loadWav( QString fileName );
60         static CAudioBuffer* loadWav( FILE *wavFile );          // support for stdio
61         void reallocate( int length );
62 
63 
getRawData()64         inline void* getRawData() { return m_data; }
getDataLength()65         inline int getDataLength() { return m_dataLength; }
66 
getBytesPerSample()67         inline int getBytesPerSample() { return (m_bitsPerSample>>3); }
getBitsPerSample()68         inline int getBitsPerSample() { return m_bitsPerSample; }
getSamplesPerSec()69         inline int getSamplesPerSec() { return m_samplesPerSec; }
getNofChannels()70         inline short getNofChannels() { return m_nofChannels; }
getSampleFunction()71         inline SAMPLE_FUNCTION_TYPE getSampleFunction() { return m_sampleFunction; }
72 
73 
74         // static implementations of sample functions
75         static AUDIO_SAMPLE_TYPE sampleFunction8bitMono( CAudioBuffer *abuffer, int pos, int channel );
76         static AUDIO_SAMPLE_TYPE sampleFunction16bitMono( CAudioBuffer *abuffer, int pos, int channel );
77         static AUDIO_SAMPLE_TYPE sampleFunction8bitStereo( CAudioBuffer *abuffer, int pos, int channel );
78         static AUDIO_SAMPLE_TYPE sampleFunction16bitStereo( CAudioBuffer *abuffer, int pos, int channel );
79 
80         CAudioBufferPlayInstance *playWithMixer( GE::CAudioMixer &mixer );
81 
82     protected:
83         SAMPLE_FUNCTION_TYPE m_sampleFunction;
84         short m_nofChannels;
85         void *m_data;
86         int m_dataLength;                // in bytes
87         short m_bitsPerSample;
88         bool m_signedData;
89         int m_samplesPerSec;
90     };
91 
92 
93 
94     class CAudioBufferPlayInstance : public IAudioSource {
95     public:
96         CAudioBufferPlayInstance();
97         CAudioBufferPlayInstance( CAudioBuffer *start_playing );
98         virtual ~CAudioBufferPlayInstance();
99         void playBuffer( CAudioBuffer *startPlaying, float volume, float fixedSpeed, int loopTimes = 0 );            // looptimes -1 = loop forever
100 
101         void setSpeed( float speed );
102         void setLeftVolume( float lvol );
103         void setRightVolume( float rvol );
104 
105 
setLoopTimes(int ltimes)106         inline void setLoopTimes( int ltimes ) { m_loopTimes = ltimes; }
107         void stop();
108 
109 
110 
111         int pullAudio( AUDIO_SAMPLE_TYPE *target, int bufferLength );
112         bool canBeDestroyed();
113 
isPlaying()114         bool isPlaying() { if (m_buffer) return true; else return false; }
isFinished()115         inline bool isFinished() { return m_finished; }
destroyWhenFinished()116         inline bool destroyWhenFinished() { return m_destroyWhenFinished; }
setDestroyWhenFinished(bool set)117         inline void setDestroyWhenFinished( bool set ) { m_destroyWhenFinished = set; }
118 
119     protected:
120         int mixBlock( AUDIO_SAMPLE_TYPE *target, int bufferLength );
121         bool m_finished;
122         bool m_destroyWhenFinished;
123         int m_fixedPos;
124         int m_fixedInc;
125 
126         int m_fixedLeftVolume;
127         int m_fixedRightVolume;
128         int m_fixedCenter;
129         int m_loopTimes;
130         CAudioBuffer *m_buffer;
131     };
132 
133 };
134 
135 
136 
137 #endif
138