1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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.
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."
39 ****************************************************************************/
41 #ifndef __GE_IGA_AUDIOBUFFER__
42 #define __GE_IGA_AUDIOBUFFER__
45 #include "GEInterfaces.h"
50 class CAudioBufferPlayInstance;
51 class CAudioBuffer; // forward declaration
52 typedef AUDIO_SAMPLE_TYPE(*SAMPLE_FUNCTION_TYPE)(CAudioBuffer *abuffer, int pos, int channel);
54 class CAudioBuffer { // container for a sound
57 virtual ~CAudioBuffer();
59 static CAudioBuffer* loadWav( QString fileName );
60 static CAudioBuffer* loadWav( FILE *wavFile ); // support for stdio
61 void reallocate( int length );
64 inline void* getRawData() { return m_data; }
65 inline int getDataLength() { return m_dataLength; }
67 inline int getBytesPerSample() { return (m_bitsPerSample>>3); }
68 inline int getBitsPerSample() { return m_bitsPerSample; }
69 inline int getSamplesPerSec() { return m_samplesPerSec; }
70 inline short getNofChannels() { return m_nofChannels; }
71 inline SAMPLE_FUNCTION_TYPE getSampleFunction() { return m_sampleFunction; }
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 );
80 CAudioBufferPlayInstance *playWithMixer( GE::CAudioMixer &mixer );
83 SAMPLE_FUNCTION_TYPE m_sampleFunction;
86 int m_dataLength; // in bytes
87 short m_bitsPerSample;
94 class CAudioBufferPlayInstance : public IAudioSource {
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
101 void setSpeed( float speed );
102 void setLeftVolume( float lvol );
103 void setRightVolume( float rvol );
106 inline void setLoopTimes( int ltimes ) { m_loopTimes = ltimes; }
111 int pullAudio( AUDIO_SAMPLE_TYPE *target, int bufferLength );
112 bool canBeDestroyed();
114 bool isPlaying() { if (m_buffer) return true; else return false; }
115 inline bool isFinished() { return m_finished; }
116 inline bool destroyWhenFinished() { return m_destroyWhenFinished; }
117 inline void setDestroyWhenFinished( bool set ) { m_destroyWhenFinished = set; }
120 int mixBlock( AUDIO_SAMPLE_TYPE *target, int bufferLength );
122 bool m_destroyWhenFinished;
126 int m_fixedLeftVolume;
127 int m_fixedRightVolume;
130 CAudioBuffer *m_buffer;