summaryrefslogtreecommitdiffstats
path: root/demos/mobile/quickhit/ga_src/GEAudioBuffer.cpp
blob: f693366bac76f1d3ff0a0004514a9dff231f4252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include <math.h>
#include "GEAudioBuffer.h"

using namespace GE;


struct SWavHeader {
    char chunkID[4];
    unsigned int chunkSize;
    char format[4];

    unsigned char subchunk1id[4];
    unsigned int subchunk1size;
    unsigned short audioFormat;
    unsigned short nofChannels;
    unsigned int sampleRate;
    unsigned int byteRate;

    unsigned short blockAlign;
    unsigned short bitsPerSample;

    unsigned char subchunk2id[4];
    unsigned int subchunk2size;

};

CAudioBuffer::CAudioBuffer() {
    m_data = 0;
    m_dataLength = 0;
    m_sampleFunction = 0;
};


CAudioBuffer::~CAudioBuffer() {
    reallocate(0);
}

void CAudioBuffer::reallocate( int length ) {
    if (m_data) delete [] ((char*)m_data);
    m_dataLength = length;
    if (m_dataLength>0) {
        m_data = new char[ m_dataLength ];
    } else m_data = 0;
};


CAudioBuffer* CAudioBuffer::loadWav( QString fileName ) {
    QFile *wavFile = new QFile( fileName );


    if (wavFile->open(QIODevice::ReadOnly)) {
        SWavHeader header;

        wavFile->read( header.chunkID, 4 );
        if (header.chunkID[0]!='R' || header.chunkID[1]!='I' || header.chunkID[2]!='F' || header.chunkID[3]!='F') return 0;    //  incorrect header

        wavFile->read( (char*)&header.chunkSize,4 );
        wavFile->read( (char*)&header.format,4 );

        if (header.format[0]!='W' || header.format[1]!='A' || header.format[2]!='V' || header.format[3]!='E') return 0;    //  incorrect header

        wavFile->read( (char*)&header.subchunk1id,4 );
        if (header.subchunk1id[0]!='f' || header.subchunk1id[1]!='m' || header.subchunk1id[2]!='t' || header.subchunk1id[3]!=' ') return 0;    //  incorrect header

        wavFile->read( (char*)&header.subchunk1size,4 );
        wavFile->read( (char*)&header.audioFormat,2 );
        wavFile->read( (char*)&header.nofChannels,2 );
        wavFile->read( (char*)&header.sampleRate,4 );
        wavFile->read( (char*)&header.byteRate,4 );
        wavFile->read( (char*)&header.blockAlign,2 );
        wavFile->read( (char*)&header.bitsPerSample,2 );

        qDebug() << fileName << " opened";

        while (1) {
            if (wavFile->read( (char*)&header.subchunk2id,4 ) != 4) return 0;
            if (wavFile->read( (char*)&header.subchunk2size,4 ) != 4) return 0;
            //int deb_size = header.subchunk2size;
            //char tes[4];
            //memcpy(tes, header.subchunk2id, 4 );
            //if (header.subchunk2id[0]!='d' || header.subchunk2id[1]!='a' || header.subchunk2id[2]!='t' || header.subchunk2id[3]!='a') return 0;    //  incorrect header
            if (header.subchunk2id[0]=='d' && header.subchunk2id[1]=='a' && header.subchunk2id[2]=='t' && header.subchunk2id[3]=='a') break;            // found the data, chunk
            // this was not the data-chunk. skip it
            if (header.subchunk2size<1) return 0;           // error in file
            char *unused = new char[header.subchunk2size];
            wavFile->read( unused, header.subchunk2size );
            delete [] unused;
        }



        // the data follows.
        if (header.subchunk2size<1) return 0;

        CAudioBuffer *rval = new CAudioBuffer;
        rval->m_nofChannels = header.nofChannels;
        rval->m_bitsPerSample = header.bitsPerSample;
        rval->m_samplesPerSec = header.sampleRate;
        rval->m_signedData = 0;        // where to know this?
        rval->reallocate( header.subchunk2size );

        wavFile->read( (char*)rval->m_data, header.subchunk2size );

        // choose a good sampling function.
        rval->m_sampleFunction = 0;
        if (rval->m_nofChannels==1) {
            if (rval->m_bitsPerSample == 8) rval->m_sampleFunction = sampleFunction8bitMono;
            if (rval->m_bitsPerSample == 16) rval->m_sampleFunction = sampleFunction16bitMono;
        } else {
            if (rval->m_bitsPerSample == 8) rval->m_sampleFunction = sampleFunction8bitStereo;
            if (rval->m_bitsPerSample == 16) rval->m_sampleFunction = sampleFunction16bitStereo;
        }

        return rval;


    } else {
        qDebug() << fileName << " NOT opened";
        return 0;
    }

    delete wavFile;
};


CAudioBuffer* CAudioBuffer::loadWav( FILE *wavFile ) {
    // read the header.
    SWavHeader header;
    fread( header.chunkID, 4, 1, wavFile );
    if (header.chunkID[0]!='R' || header.chunkID[1]!='I' || header.chunkID[2]!='F' || header.chunkID[3]!='F') return 0;    //  incorrect header

    fread( &header.chunkSize, 4, 1, wavFile );
    fread( header.format, 4, 1, wavFile );
    if (header.format[0]!='W' || header.format[1]!='A' || header.format[2]!='V' || header.format[3]!='E') return 0;    //  incorrect header

    fread( header.subchunk1id, 4, 1, wavFile );
    if (header.subchunk1id[0]!='f' || header.subchunk1id[1]!='m' || header.subchunk1id[2]!='t' || header.subchunk1id[3]!=' ') return 0;    //  incorrect header

    fread( &header.subchunk1size, 4, 1, wavFile );
    fread( &header.audioFormat, 2, 1, wavFile );
    fread( &header.nofChannels, 2, 1, wavFile );
    fread( &header.sampleRate, 4, 1, wavFile );
    fread( &header.byteRate, 4, 1, wavFile );

    fread( &header.blockAlign, 2, 1, wavFile );
    fread( &header.bitsPerSample, 2, 1, wavFile );

    fread( header.subchunk2id, 4, 1, wavFile );
    if (header.subchunk2id[0]!='d' || header.subchunk2id[1]!='a' || header.subchunk2id[2]!='t' || header.subchunk2id[3]!='a') return 0;    //  incorrect header
    fread( &header.subchunk2size, 4, 1, wavFile );


    // the data follows.
    if (header.subchunk2size<1) return 0;

    CAudioBuffer *rval = new CAudioBuffer;
    rval->m_nofChannels = header.nofChannels;
    rval->m_bitsPerSample = header.bitsPerSample;
    rval->m_samplesPerSec = header.sampleRate;
    rval->m_signedData = 0;        // where to know this?
    rval->reallocate( header.subchunk2size );

    fread( rval->m_data, 1, header.subchunk2size, wavFile );



    // choose a good sampling function.
    rval->m_sampleFunction = 0;
    if (rval->m_nofChannels==1) {
        if (rval->m_bitsPerSample == 8) rval->m_sampleFunction = sampleFunction8bitMono;
        if (rval->m_bitsPerSample == 16) rval->m_sampleFunction = sampleFunction16bitMono;
    } else {
        if (rval->m_bitsPerSample == 8) rval->m_sampleFunction = sampleFunction8bitStereo;
        if (rval->m_bitsPerSample == 16) rval->m_sampleFunction = sampleFunction16bitStereo;
    }

    return rval;
};



AUDIO_SAMPLE_TYPE CAudioBuffer::sampleFunction8bitMono( CAudioBuffer *abuffer, int pos, int channel ) {
    return (AUDIO_SAMPLE_TYPE)(((unsigned char*)(abuffer->m_data))[pos]-128)<<8;
};

AUDIO_SAMPLE_TYPE CAudioBuffer::sampleFunction16bitMono( CAudioBuffer *abuffer, int pos, int channel ) {
    return (AUDIO_SAMPLE_TYPE)(((short*)(abuffer->m_data))[pos]);
};

AUDIO_SAMPLE_TYPE CAudioBuffer::sampleFunction8bitStereo( CAudioBuffer *abuffer, int pos, int channel ) {
    return ((AUDIO_SAMPLE_TYPE)(((char*)(abuffer->m_data))[pos*abuffer->m_nofChannels + channel])<<8);
};


AUDIO_SAMPLE_TYPE CAudioBuffer::sampleFunction16bitStereo( CAudioBuffer *abuffer, int pos, int channel ) {
    return (AUDIO_SAMPLE_TYPE)(((short*)(abuffer->m_data))[pos*abuffer->m_nofChannels + channel]);
};

CAudioBufferPlayInstance *CAudioBuffer::playWithMixer( CAudioMixer &mixer ) {
    CAudioBufferPlayInstance *i = (CAudioBufferPlayInstance*)mixer.addAudioSource( new CAudioBufferPlayInstance( this ));
    return i;
};


CAudioBufferPlayInstance::CAudioBufferPlayInstance() {
    m_fixedPos = 0;
    m_fixedInc = 0;
    m_buffer = 0;
    m_fixedLeftVolume = 4096;
    m_fixedRightVolume = 4096;
    m_destroyWhenFinished = true;
    m_finished = false;
};

CAudioBufferPlayInstance::CAudioBufferPlayInstance( CAudioBuffer *startPlaying ) {
    m_fixedPos = 0;
    m_fixedInc = 0;
    m_fixedLeftVolume = 4096;
    m_fixedRightVolume = 4096;
    m_destroyWhenFinished = true;
    m_finished = false;
    playBuffer( startPlaying, 1.0f, 1.0f );
};

void CAudioBufferPlayInstance::playBuffer( CAudioBuffer *startPlaying, float volume, float speed, int loopTimes ) {
    m_buffer = startPlaying;
    m_fixedLeftVolume = (int)(4096.0f*volume);
    m_fixedRightVolume = m_fixedLeftVolume;
    m_fixedPos = 0;
    //m_fixedInc = ( startPlaying->getSamplesPerSec() * (int)(4096.0f*speed)) / AUDIO_FREQUENCY;
    setSpeed( speed );
    m_loopTimes = loopTimes;

};

CAudioBufferPlayInstance::~CAudioBufferPlayInstance() {

};


void CAudioBufferPlayInstance::stop() {
    m_buffer = 0;
    m_finished = true;
};

void CAudioBufferPlayInstance::setSpeed( float speed ) {
    if (!m_buffer) return;
    m_fixedInc = (int)( ((float)m_buffer->getSamplesPerSec() * 4096.0f*speed) / (float)AUDIO_FREQUENCY );
};

void CAudioBufferPlayInstance::setLeftVolume( float vol ) {
    m_fixedLeftVolume = (int)(4096.0f*vol);
};

void CAudioBufferPlayInstance::setRightVolume( float vol ) {
    m_fixedRightVolume = (int)(4096.0f*vol);
};

bool CAudioBufferPlayInstance::canBeDestroyed() {
    if (m_finished==true &&
        m_destroyWhenFinished==true) return true; else return false;
};



// Does not do any bound-checking. Must be checked before called.
int CAudioBufferPlayInstance::mixBlock( AUDIO_SAMPLE_TYPE *target, int samplesToMix ) {
    SAMPLE_FUNCTION_TYPE sampleFunction = m_buffer->getSampleFunction();
    if (!sampleFunction) return 0; // unsupported sampletype
    AUDIO_SAMPLE_TYPE *t_target = target+samplesToMix*2;
    int sourcepos;
    //int tempCounter = 0;

    if (m_buffer->getNofChannels() == 2) {         // stereo
        while (target!=t_target) {
            sourcepos = m_fixedPos>>12;
            target[0] = (((((sampleFunction)( m_buffer, sourcepos, 0) * (4096-(m_fixedPos&4095)) + (sampleFunction)( m_buffer, sourcepos+1, 0) * (m_fixedPos&4095) ) >> 12) * m_fixedLeftVolume) >> 12);
            target[1] = (((((sampleFunction)( m_buffer, sourcepos, 1) * (4096-(m_fixedPos&4095)) + (sampleFunction)( m_buffer, sourcepos+1, 1) * (m_fixedPos&4095) ) >> 12) * m_fixedRightVolume) >> 12);
            m_fixedPos+=m_fixedInc;
            target+=2;
            //tempCounter++;
        };
    } else {                                      // mono
        int temp;
        while (target!=t_target) {
            sourcepos = m_fixedPos>>12;
            temp = (((sampleFunction)( m_buffer, sourcepos, 0 ) * (4096-(m_fixedPos&4095)) + (sampleFunction)( m_buffer, sourcepos+1, 0 ) * (m_fixedPos&4095) ) >> 12);
            target[0] = ((temp*m_fixedLeftVolume)>>12);
            target[1] = ((temp*m_fixedRightVolume)>>12);
            m_fixedPos+=m_fixedInc;
            target+=2;
            //tempCounter++;
        };

    };

    return samplesToMix;
};



int CAudioBufferPlayInstance::pullAudio( AUDIO_SAMPLE_TYPE *target, int bufferLength ) {
    if (!m_buffer) return 0;            // no sample associated to mix..

    int channelLength = ((m_buffer->getDataLength()) / (m_buffer->getNofChannels()*m_buffer->getBytesPerSample()))-2;

    int samplesToWrite = bufferLength/2;
    int amount;
    int totalMixed = 0;


    while (samplesToWrite>0) {
        int samplesLeft = channelLength - (m_fixedPos>>12);
        int maxMixAmount = (int)(((long long int)(samplesLeft)<<12) / m_fixedInc );         // This is how much we can mix at least
        //int maxMixAmount = (int)((float)samplesLeft / ((float)m_fixedInc/4096.0f));
        //if (maxMixAmount<1) maxMixAmount = 1;           // NOTE, THIS MIGHT CAUSE PROBLEMS. NEEDS CHECKING
        if (maxMixAmount>samplesToWrite) {
            maxMixAmount=samplesToWrite;
        }

        if (maxMixAmount > 0) {
            amount=mixBlock(target+totalMixed * 2, maxMixAmount);

            if (amount == 0)
            {
                // Error!
                break;
            }

            totalMixed+=amount;
        } else {
            amount = 0;
            m_fixedPos = channelLength<<12;
        }

        // sample is ended,.. check the looping variables and see what to do.
        if ((m_fixedPos>>12)>=channelLength) {
            m_fixedPos -= (channelLength<<12);
            if (m_loopTimes>0) m_loopTimes--;
            if (m_loopTimes==0) {
                stop();
                return totalMixed;
            }
        }

        samplesToWrite-=amount;
        if (samplesToWrite<1) break;
    };
    return  totalMixed*2;
};