~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/src/audio/audiorecord.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
21
 *  If you modify this program, or any covered work, by linking or
 
22
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
23
 *  modified version of that library), containing parts covered by the
 
24
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
25
 *  grants you additional permission to convey the resulting work.
 
26
 *  Corresponding Source for a non-source form of such a combination
 
27
 *  shall include the source code for the parts of OpenSSL used as well
 
28
 *  as that of the covered work.
 
29
 */
 
30
 
 
31
#include "audiorecord.h"
 
32
 
 
33
// structure for the wave header
 
34
 
 
35
struct wavhdr {
 
36
    char riff[4];           // "RIFF"
 
37
    SINT32 file_size;       // in bytes
 
38
    char wave[4];           // "WAVE"
 
39
    char fmt[4];            // "fmt "
 
40
    SINT32 chunk_size;      // in bytes (16 for PCM)
 
41
    SINT16 format_tag;      // 1=PCM, 2=ADPCM, 3=IEEE float, 6=A-Law, 7=Mu-Law
 
42
    SINT16 num_chans;       // 1=mono, 2=stereo
 
43
    SINT32 sample_rate;
 
44
    SINT32 bytes_per_sec;
 
45
    SINT16 bytes_per_samp;  // 2=16-bit mono, 4=16-bit stereo
 
46
    SINT16 bits_per_samp;
 
47
    char data[4];           // "data"
 
48
    SINT32 data_length;     // in bytes
 
49
};
 
50
 
 
51
 
 
52
AudioRecord::AudioRecord() : fp (NULL)
 
53
    , channels_ (1)
 
54
    , byteCounter_ (0)
 
55
    , sndSmplRate_ (8000)
 
56
    , nbSamplesMic_ (0)
 
57
    , nbSamplesSpk_ (0)
 
58
    , nbSamplesMax_ (3000)
 
59
    , recordingEnabled_ (false)
 
60
    , mixBuffer_ (NULL)
 
61
    , micBuffer_ (NULL)
 
62
    , spkBuffer_ (NULL)
 
63
{
 
64
 
 
65
    mixBuffer_ = new SFLDataFormat[nbSamplesMax_];
 
66
    micBuffer_ = new SFLDataFormat[nbSamplesMax_];
 
67
    spkBuffer_ = new SFLDataFormat[nbSamplesMax_];
 
68
 
 
69
    createFilename();
 
70
}
 
71
 
 
72
AudioRecord::~AudioRecord()
 
73
{
 
74
    delete [] mixBuffer_;
 
75
    delete [] micBuffer_;
 
76
    delete [] spkBuffer_;
 
77
}
 
78
 
 
79
 
 
80
void AudioRecord::setSndSamplingRate (int smplRate)
 
81
{
 
82
    sndSmplRate_ = smplRate;
 
83
}
 
84
 
 
85
void AudioRecord::setRecordingOption (FILE_TYPE type, SOUND_FORMAT format, int sndSmplRate, std::string path)
 
86
{
 
87
 
 
88
    fileType_ = type;
 
89
    sndFormat_ = format;
 
90
    channels_ = 1;
 
91
    sndSmplRate_ = sndSmplRate;
 
92
 
 
93
    savePath_ = path + "/";
 
94
 
 
95
}
 
96
 
 
97
 
 
98
 
 
99
void AudioRecord::initFileName (std::string peerNumber)
 
100
{
 
101
 
 
102
    std::string fName;
 
103
 
 
104
    fName = fileName_;
 
105
    fName.append ("-"+peerNumber);
 
106
 
 
107
    if (fileType_ == FILE_RAW) {
 
108
        if (strstr (fileName_, ".raw") == NULL) {
 
109
            _debug ("AudioRecord: concatenate .raw file extension: name : %s", fileName_);
 
110
            fName.append (".raw");
 
111
        }
 
112
    } else if (fileType_ == FILE_WAV) {
 
113
        if (strstr (fileName_, ".wav") == NULL) {
 
114
            _debug ("AudioRecord: concatenate .wav file extension: name : %s", fileName_);
 
115
            fName.append (".wav");
 
116
        }
 
117
    }
 
118
 
 
119
    savePath_.append (fName);
 
120
}
 
121
 
 
122
void AudioRecord::openFile()
 
123
{
 
124
 
 
125
    bool result = false;
 
126
 
 
127
    _debug ("AudioRecord: Open file()");
 
128
 
 
129
    if (isFileExist()) {
 
130
        _debug ("AudioRecord: Filename does not exist, creating one");
 
131
        byteCounter_ = 0;
 
132
 
 
133
        if (fileType_ == FILE_RAW) {
 
134
            result = setRawFile();
 
135
        } else if (fileType_ == FILE_WAV) {
 
136
            result = setWavFile();
 
137
        }
 
138
    } else {
 
139
        _debug ("AudioRecord: Filename already exist opening it");
 
140
 
 
141
        if (fileType_ == FILE_RAW) {
 
142
            result = openExistingRawFile();
 
143
        } else if (fileType_ == FILE_WAV) {
 
144
            result = openExistingWavFile();
 
145
        }
 
146
    }
 
147
}
 
148
 
 
149
 
 
150
void AudioRecord::closeFile()
 
151
{
 
152
 
 
153
    if (fp == 0) return;
 
154
 
 
155
    if (fileType_ == FILE_RAW)
 
156
        fclose (fp);
 
157
    else if (fileType_ == FILE_WAV)
 
158
        this->closeWavFile();
 
159
 
 
160
 
 
161
 
 
162
}
 
163
 
 
164
 
 
165
bool AudioRecord::isOpenFile()
 
166
{
 
167
 
 
168
    if (fp) {
 
169
        return true;
 
170
    } else {
 
171
        return false;
 
172
    }
 
173
}
 
174
 
 
175
 
 
176
bool AudioRecord::isFileExist()
 
177
{
 
178
    _info ("AudioRecord: Try to open name : %s ", fileName_);
 
179
 
 
180
    if (fopen (fileName_,"rb") ==0) {
 
181
        return true;
 
182
    }
 
183
 
 
184
    return false;
 
185
}
 
186
 
 
187
bool AudioRecord::isRecording()
 
188
{
 
189
 
 
190
    if (recordingEnabled_)
 
191
        return true;
 
192
    else
 
193
        return false;
 
194
}
 
195
 
 
196
 
 
197
bool AudioRecord::setRecording()
 
198
{
 
199
 
 
200
    if (isOpenFile()) {
 
201
        if (!recordingEnabled_) {
 
202
            _info ("AudioRecording: Start recording");
 
203
            recordingEnabled_ = true;
 
204
        } else {
 
205
            recordingEnabled_ = false;
 
206
            _info ("AudioRecording: Stop recording");
 
207
        }
 
208
    } else {
 
209
        openFile();
 
210
 
 
211
        recordingEnabled_ = true; // once opend file, start recording
 
212
    }
 
213
 
 
214
    // WARNING: Unused return value
 
215
    return true;
 
216
 
 
217
}
 
218
 
 
219
void AudioRecord::stopRecording()
 
220
{
 
221
    _info ("AudioRecording: Stop recording");
 
222
 
 
223
    if (recordingEnabled_)
 
224
        recordingEnabled_ = false;
 
225
}
 
226
 
 
227
 
 
228
void AudioRecord::createFilename()
 
229
{
 
230
 
 
231
    time_t rawtime;
 
232
 
 
233
    struct tm * timeinfo;
 
234
 
 
235
    rawtime = time (NULL);
 
236
    timeinfo = localtime (&rawtime);
 
237
 
 
238
    stringstream out;
 
239
 
 
240
    // DATE
 
241
    out << timeinfo->tm_year+1900;
 
242
 
 
243
    if (timeinfo->tm_mon < 9) // january is 01, not 1
 
244
        out << 0;
 
245
 
 
246
    out << timeinfo->tm_mon+1;
 
247
 
 
248
    if (timeinfo->tm_mday < 10) // 01 02 03, not 1 2 3
 
249
        out << 0;
 
250
 
 
251
    out << timeinfo->tm_mday;
 
252
 
 
253
    out << '-';
 
254
 
 
255
    // hour
 
256
    if (timeinfo->tm_hour < 10) // 01 02 03, not 1 2 3
 
257
        out << 0;
 
258
 
 
259
    out << timeinfo->tm_hour;
 
260
 
 
261
    out << ':';
 
262
 
 
263
    if (timeinfo->tm_min < 10) // 01 02 03, not 1 2 3
 
264
        out << 0;
 
265
 
 
266
    out << timeinfo->tm_min;
 
267
 
 
268
    out << ':';
 
269
 
 
270
    if (timeinfo->tm_sec < 10) // 01 02 03,  not 1 2 3
 
271
        out << 0;
 
272
 
 
273
    out << timeinfo->tm_sec;
 
274
 
 
275
    // fileName_ = out.str();
 
276
    strncpy (fileName_, out.str().c_str(), 8192);
 
277
 
 
278
    _info ("AudioRecord: create filename for this call %s ", fileName_);
 
279
}
 
280
 
 
281
bool AudioRecord::setRawFile()
 
282
{
 
283
 
 
284
    fp = fopen (savePath_.c_str(), "wb");
 
285
 
 
286
    if (!fp) {
 
287
        _warn ("AudioRecord::setRawFile() : could not create RAW file!");
 
288
        return false;
 
289
    }
 
290
 
 
291
    if (sndFormat_ != INT16) {   // TODO need to change INT16 to SINT16
 
292
        sndFormat_ = INT16;
 
293
        _debug ("AudioRecord::setRawFile() : using 16-bit signed integer data format for file.");
 
294
    }
 
295
 
 
296
    _debug ("AudioRecord:setRawFile() : created RAW file.");
 
297
 
 
298
    return true;
 
299
}
 
300
 
 
301
 
 
302
bool AudioRecord::setWavFile()
 
303
{
 
304
    _debug ("AudioRecord: Create wave file %s", savePath_.c_str());
 
305
 
 
306
    fp = fopen (savePath_.c_str(), "wb");
 
307
 
 
308
    if (!fp) {
 
309
        _warn ("AudioRecord: Error: could not create WAV file.");
 
310
        return false;
 
311
    }
 
312
 
 
313
    struct wavhdr hdr = {"RIF", 44, "WAV", "fmt", 16, 1, 1,
 
314
        sndSmplRate_, 0, 2, 16, "dat", 0
 
315
    };
 
316
 
 
317
    hdr.riff[3] = 'F';
 
318
 
 
319
    hdr.wave[3] = 'E';
 
320
 
 
321
    hdr.fmt[3]  = ' ';
 
322
 
 
323
    hdr.data[3] = 'a';
 
324
 
 
325
    hdr.num_chans = channels_;
 
326
 
 
327
    if (sndFormat_ == INT16) {   //  TODO need to write INT16 to SINT16
 
328
        hdr.bits_per_samp = 16;
 
329
    }
 
330
 
 
331
    hdr.bytes_per_samp = (SINT16) (channels_ * hdr.bits_per_samp / 8);
 
332
 
 
333
    hdr.bytes_per_sec = (SINT32) (hdr.sample_rate * hdr.bytes_per_samp);
 
334
 
 
335
 
 
336
    if (fwrite (&hdr, 4, 11, fp) != 11) {
 
337
        _warn ("AudioRecord: Error: could not write WAV header for file. ");
 
338
        return false;
 
339
    }
 
340
 
 
341
    _debug ("AudioRecord: created WAV file successfully.");
 
342
 
 
343
    return true;
 
344
}
 
345
 
 
346
 
 
347
bool AudioRecord::openExistingRawFile()
 
348
{
 
349
    fp = fopen (fileName_, "ab+");
 
350
 
 
351
    if (!fp) {
 
352
        _warn ("AudioRecord: could not create RAW file!");
 
353
        return false;
 
354
    }
 
355
 
 
356
    return true;
 
357
}
 
358
 
 
359
 
 
360
bool AudioRecord::openExistingWavFile()
 
361
{
 
362
    _info ("AudioRecord: Open existing wave file");
 
363
 
 
364
    fp = fopen (fileName_, "rb+");
 
365
 
 
366
    if (!fp) {
 
367
        _warn ("AudioRecord: Error: could not open WAV file!");
 
368
        return false;
 
369
    }
 
370
 
 
371
    printf ("AudioRecord::openExistingWavFile()::Tried to open %s ",fileName_);
 
372
 
 
373
    if (fseek (fp, 40, SEEK_SET) != 0) // jump to data length
 
374
        _warn ("AudioRecord: Error: Couldn't seek offset 40 in the file ");
 
375
 
 
376
    if (fread (&byteCounter_, 4, 1, fp))
 
377
        _warn ("AudioRecord: Error: bytecounter Read successfully ");
 
378
 
 
379
    if (fseek (fp, 0 , SEEK_END) != 0)
 
380
        _warn ("AudioRecord: Error: Couldn't seek at the en of the file ");
 
381
 
 
382
 
 
383
    if (fclose (fp) != 0)
 
384
        _warn ("AudioRecord: Error: Can't close file r+ ");
 
385
 
 
386
 
 
387
 
 
388
    fp = fopen (fileName_, "ab+");
 
389
 
 
390
    if (!fp) {
 
391
        _warn ("AudioRecord: Error: Could not createopen WAV file ab+!");
 
392
        return false;
 
393
    }
 
394
 
 
395
    if (fseek (fp, 4 , SEEK_END) != 0)
 
396
        _warn ("AudioRecord: Error: Couldn't seek at the en of the file ");
 
397
 
 
398
    return true;
 
399
 
 
400
}
 
401
 
 
402
 
 
403
void AudioRecord::closeWavFile()
 
404
{
 
405
    if (fp == 0) {
 
406
        _debug ("AudioRecord: Can't closeWavFile, a file has not yet been opened!");
 
407
        return;
 
408
    }
 
409
 
 
410
    _debug ("AudioRecord: Close wave file");
 
411
 
 
412
 
 
413
    SINT32 bytes = byteCounter_ * channels_;
 
414
 
 
415
    fseek (fp, 40, SEEK_SET); // jump to data length
 
416
 
 
417
    if (ferror (fp))
 
418
        _warn ("AudioRecord: Error: can't reach offset 40 while closing");
 
419
 
 
420
    fwrite (&bytes, sizeof (SINT32), 1, fp);
 
421
 
 
422
    if (ferror (fp))
 
423
        _warn ("AudioRecord: Error: can't write bytes for data length ");
 
424
 
 
425
 
 
426
    bytes = byteCounter_ * channels_ + 44; // + 44 for the wave header
 
427
 
 
428
    fseek (fp, 4, SEEK_SET); // jump to file size
 
429
 
 
430
    if (ferror (fp))
 
431
        _warn ("AudioRecord: Error: can't reach offset 4");
 
432
 
 
433
    fwrite (&bytes, 4, 1, fp);
 
434
 
 
435
    if (ferror (fp))
 
436
        _warn ("AudioRecord: Error: can't reach offset 4");
 
437
 
 
438
 
 
439
    if (fclose (fp) != 0)
 
440
        _warn ("AudioRecord: Error: can't close file");
 
441
 
 
442
 
 
443
}
 
444
 
 
445
void AudioRecord::recSpkrData (SFLDataFormat* buffer, int nSamples)
 
446
{
 
447
 
 
448
    if (recordingEnabled_) {
 
449
 
 
450
        nbSamplesMic_ = nSamples;
 
451
 
 
452
        for (int i = 0; i < nbSamplesMic_; i++)
 
453
            micBuffer_[i] = buffer[i];
 
454
    }
 
455
 
 
456
    return;
 
457
}
 
458
 
 
459
 
 
460
void AudioRecord::recMicData (SFLDataFormat* buffer, int nSamples)
 
461
{
 
462
 
 
463
    if (recordingEnabled_) {
 
464
 
 
465
        nbSamplesSpk_ = nSamples;
 
466
 
 
467
        for (int i = 0; i < nbSamplesSpk_; i++)
 
468
            spkBuffer_[i] = buffer[i];
 
469
 
 
470
    }
 
471
 
 
472
    return;
 
473
}
 
474
 
 
475
 
 
476
void AudioRecord::recData (SFLDataFormat* buffer, int nSamples)
 
477
{
 
478
 
 
479
    if (recordingEnabled_) {
 
480
 
 
481
        if (fp == 0) {
 
482
            _debug ("AudioRecord: Can't record data, a file has not yet been opened!");
 
483
            return;
 
484
        }
 
485
 
 
486
 
 
487
 
 
488
        if (sndFormat_ == INT16) {   // TODO change INT16 to SINT16
 
489
            if (fwrite (buffer, sizeof (SFLDataFormat), nSamples, fp) != (unsigned int) nSamples)
 
490
                _warn ("AudioRecord: Could not record data! ");
 
491
            else {
 
492
                fflush (fp);
 
493
                byteCounter_ += (unsigned long) (nSamples*sizeof (SFLDataFormat));
 
494
            }
 
495
        }
 
496
    }
 
497
 
 
498
    return;
 
499
}
 
500
 
 
501
 
 
502
void AudioRecord::recData (SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int nSamples_1, int nSamples_2 UNUSED)
 
503
{
 
504
 
 
505
    if (recordingEnabled_) {
 
506
 
 
507
        _debug ("Recording enabled");
 
508
 
 
509
        if (fp == 0) {
 
510
            _debug ("AudioRecord: Can't record data, a file has not yet been opened!");
 
511
            return;
 
512
        }
 
513
 
 
514
 
 
515
        if (sndFormat_ == INT16) {   // TODO change INT16 to SINT16
 
516
            for (int k=0; k<nSamples_1; k++) {
 
517
 
 
518
                mixBuffer_[k] = (buffer_1[k]+buffer_2[k]);
 
519
 
 
520
 
 
521
                if (fwrite (&mixBuffer_[k], 2, 1, fp) != 1)
 
522
                    _warn ("AudioRecord: Could not record data!");
 
523
                else {
 
524
                    fflush (fp);
 
525
                }
 
526
            }
 
527
        }
 
528
 
 
529
        byteCounter_ += (unsigned long) (nSamples_1*sizeof (SFLDataFormat));
 
530
 
 
531
    }
 
532
 
 
533
    return;
 
534
}
 
535