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

« back to all changes in this revision

Viewing changes to sflphone-common/src/plug-in/audiorecorder/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()
53
 
{
54
 
 
55
 
    sndSmplRate_ = 8000;
56
 
    channels_ = 1;
57
 
    byteCounter_ = 0;
58
 
    recordingEnabled_ = false;
59
 
    fp = 0;
60
 
    nbSamplesMax_ = 3000;
61
 
 
62
 
    createFilename();
63
 
 
64
 
    mixBuffer_ = new SFLDataFormat[nbSamplesMax_];
65
 
    micBuffer_ = new SFLDataFormat[nbSamplesMax_];
66
 
    spkBuffer_ = new SFLDataFormat[nbSamplesMax_];
67
 
}
68
 
 
69
 
AudioRecord::~AudioRecord()
70
 
{
71
 
    delete [] mixBuffer_;
72
 
    delete [] micBuffer_;
73
 
    delete [] spkBuffer_;
74
 
}
75
 
 
76
 
 
77
 
void AudioRecord::setSndSamplingRate (int smplRate)
78
 
{
79
 
    sndSmplRate_ = smplRate;
80
 
}
81
 
 
82
 
void AudioRecord::setRecordingOption (FILE_TYPE type, SOUND_FORMAT format, int sndSmplRate, std::string path)
83
 
{
84
 
 
85
 
 
86
 
    fileType_ = type;
87
 
    sndFormat_ = format;
88
 
    channels_ = 1;
89
 
    sndSmplRate_ = sndSmplRate;
90
 
 
91
 
    savePath_ = path + "/";
92
 
 
93
 
}
94
 
 
95
 
 
96
 
 
97
 
void AudioRecord::initFileName (std::string peerNumber)
98
 
{
99
 
 
100
 
    std::string fName;
101
 
 
102
 
    fName = fileName_;
103
 
    fName.append ("-"+peerNumber);
104
 
 
105
 
    if (fileType_ == FILE_RAW) {
106
 
        if (strstr (fileName_, ".raw") == NULL) {
107
 
            _debug("AudioRecord: concatenate .raw file extension: name : %s", fileName_);
108
 
            fName.append (".raw");
109
 
        }
110
 
    } else if (fileType_ == FILE_WAV) {
111
 
        if (strstr (fileName_, ".wav") == NULL) {
112
 
            _debug("AudioRecord: concatenate .wav file extension: name : %s", fileName_);
113
 
            fName.append (".wav");
114
 
        }
115
 
    }
116
 
 
117
 
    savePath_.append (fName);
118
 
}
119
 
 
120
 
void AudioRecord::openFile()
121
 
{
122
 
 
123
 
    _info ("AudioRecord: Open file()");
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
 
      }
205
 
      else {
206
 
          recordingEnabled_ = false;
207
 
          _info ("AudioRecording: Stop recording");
208
 
      }
209
 
    } 
210
 
    else {
211
 
        openFile();
212
 
        
213
 
        recordingEnabled_ = true; // once opend file, start recording
214
 
    }
215
 
 
216
 
    // WARNING: Unused return value
217
 
    return true;
218
 
 
219
 
}
220
 
 
221
 
void AudioRecord::stopRecording()
222
 
{
223
 
    _info ("AudioRecording: Stop recording");
224
 
 
225
 
    if (recordingEnabled_)
226
 
        recordingEnabled_ = false;
227
 
}
228
 
 
229
 
 
230
 
void AudioRecord::createFilename()
231
 
{
232
 
 
233
 
    time_t rawtime;
234
 
 
235
 
    struct tm * timeinfo;
236
 
 
237
 
    rawtime = time (NULL);
238
 
    timeinfo = localtime (&rawtime);
239
 
 
240
 
    stringstream out;
241
 
 
242
 
    // DATE
243
 
    out << timeinfo->tm_year+1900;
244
 
 
245
 
    if (timeinfo->tm_mon < 9) // january is 01, not 1
246
 
        out << 0;
247
 
 
248
 
    out << timeinfo->tm_mon+1;
249
 
 
250
 
    if (timeinfo->tm_mday < 10) // 01 02 03, not 1 2 3
251
 
        out << 0;
252
 
 
253
 
    out << timeinfo->tm_mday;
254
 
 
255
 
    out << '-';
256
 
 
257
 
    // hour
258
 
    if (timeinfo->tm_hour < 10) // 01 02 03, not 1 2 3
259
 
        out << 0;
260
 
 
261
 
    out << timeinfo->tm_hour;
262
 
 
263
 
    out << ':';
264
 
 
265
 
    if (timeinfo->tm_min < 10) // 01 02 03, not 1 2 3
266
 
        out << 0;
267
 
 
268
 
    out << timeinfo->tm_min;
269
 
 
270
 
    out << ':';
271
 
 
272
 
    if (timeinfo->tm_sec < 10) // 01 02 03,  not 1 2 3
273
 
        out << 0;
274
 
 
275
 
    out << timeinfo->tm_sec;
276
 
 
277
 
    // fileName_ = out.str();
278
 
    strncpy (fileName_, out.str().c_str(), 8192);
279
 
 
280
 
    _info("AudioRecord: create filename for this call %s ", fileName_);
281
 
}
282
 
 
283
 
bool AudioRecord::setRawFile()
284
 
{
285
 
 
286
 
    fp = fopen (savePath_.c_str(), "wb");
287
 
 
288
 
    if (!fp) {
289
 
        _warn ("AudioRecord::setRawFile() : could not create RAW file!");
290
 
        return false;
291
 
    }
292
 
 
293
 
    if (sndFormat_ != INT16) {   // TODO need to change INT16 to SINT16
294
 
        sndFormat_ = INT16;
295
 
        _debug ("AudioRecord::setRawFile() : using 16-bit signed integer data format for file.");
296
 
    }
297
 
 
298
 
    _debug ("AudioRecord:setRawFile() : created RAW file.");
299
 
 
300
 
    return true;
301
 
}
302
 
 
303
 
 
304
 
bool AudioRecord::setWavFile()
305
 
{
306
 
 
307
 
    fp = fopen (savePath_.c_str(), "wb");
308
 
 
309
 
    if (!fp) {
310
 
        _warn("AudioRecord: Error: could not create WAV file.");
311
 
        return false;
312
 
    }
313
 
 
314
 
    struct wavhdr hdr = {"RIF", 44, "WAV", "fmt", 16, 1, 1,
315
 
        sndSmplRate_, 0, 2, 16, "dat", 0
316
 
    };
317
 
 
318
 
    hdr.riff[3] = 'F';
319
 
 
320
 
    hdr.wave[3] = 'E';
321
 
 
322
 
    hdr.fmt[3]  = ' ';
323
 
 
324
 
    hdr.data[3] = 'a';
325
 
 
326
 
    hdr.num_chans = channels_;
327
 
 
328
 
    if (sndFormat_ == INT16) {   //  TODO need to write INT16 to SINT16
329
 
        hdr.bits_per_samp = 16;
330
 
    }
331
 
 
332
 
    hdr.bytes_per_samp = (SINT16) (channels_ * hdr.bits_per_samp / 8);
333
 
 
334
 
    hdr.bytes_per_sec = (SINT32) (hdr.sample_rate * hdr.bytes_per_samp);
335
 
 
336
 
 
337
 
    if (fwrite (&hdr, 4, 11, fp) != 11) {
338
 
        _warn("AudioRecord: Error: could not write WAV header for file. ");
339
 
        return false;
340
 
    }
341
 
 
342
 
    _debug ("AudioRecord: created WAV file successfully.");
343
 
 
344
 
    return true;
345
 
}
346
 
 
347
 
 
348
 
bool AudioRecord::openExistingRawFile()
349
 
{
350
 
    fp = fopen (fileName_, "ab+");
351
 
 
352
 
    if (!fp) {
353
 
        _warn ("AudioRecord: could not create RAW file!");
354
 
        return false;
355
 
    }
356
 
 
357
 
    return true;
358
 
}
359
 
 
360
 
 
361
 
bool AudioRecord::openExistingWavFile()
362
 
{
363
 
    _info ("AudioRecord: Open existing wave file");
364
 
 
365
 
    fp = fopen (fileName_, "rb+");
366
 
 
367
 
    if (!fp) {
368
 
        _warn("AudioRecord: Error: could not open WAV file!");
369
 
        return false;
370
 
    }
371
 
 
372
 
    printf ("AudioRecord::openExistingWavFile()::Tried to open %s ",fileName_);
373
 
 
374
 
    if (fseek (fp, 40, SEEK_SET) != 0) // jump to data length
375
 
        _warn ("AudioRecord: Error: Couldn't seek offset 40 in the file ");
376
 
 
377
 
    if (fread (&byteCounter_, 4, 1, fp))
378
 
        _warn("AudioRecord: Error: bytecounter Read successfully ");
379
 
 
380
 
    if (fseek (fp, 0 , SEEK_END) != 0)
381
 
        _warn ("AudioRecord: Error: Couldn't seek at the en of the file ");
382
 
 
383
 
 
384
 
    if (fclose (fp) != 0)
385
 
        _warn ("AudioRecord: Error: Can't close file r+ ");
386
 
 
387
 
 
388
 
 
389
 
    fp = fopen (fileName_, "ab+");
390
 
 
391
 
    if (!fp) {
392
 
        _warn ("AudioRecord: Error: Could not createopen WAV file ab+!");
393
 
        return false;
394
 
    }
395
 
 
396
 
    if (fseek (fp, 4 , SEEK_END) != 0)
397
 
        _warn ("AudioRecord: Error: Couldn't seek at the en of the file ");
398
 
 
399
 
    return true;
400
 
 
401
 
}
402
 
 
403
 
 
404
 
void AudioRecord::closeWavFile()
405
 
{
406
 
    if (fp == 0) {
407
 
        _debug ("AudioRecord: Can't closeWavFile, a file has not yet been opened!");
408
 
        return;
409
 
    }
410
 
 
411
 
 
412
 
    SINT32 bytes = byteCounter_ * channels_;
413
 
 
414
 
    fseek (fp, 40, SEEK_SET); // jump to data length
415
 
    if (ferror (fp)) 
416
 
        _warn ("AudioRecord: Error: can't reach offset 40 while closing");
417
 
 
418
 
    fwrite (&bytes, sizeof (SINT32), 1, fp);
419
 
    if (ferror (fp)) 
420
 
        _warn ("AudioRecord: Error: can't write bytes for data length ");
421
 
 
422
 
 
423
 
    bytes = byteCounter_ * channels_ + 44; // + 44 for the wave header
424
 
 
425
 
    fseek (fp, 4, SEEK_SET); // jump to file size
426
 
    if (ferror (fp)) 
427
 
        _warn ("AudioRecord: Error: can't reach offset 4");
428
 
 
429
 
    fwrite (&bytes, 4, 1, fp);
430
 
    if (ferror (fp)) 
431
 
        _warn("AudioRecord: Error: can't reach offset 4");
432
 
 
433
 
 
434
 
    if (fclose (fp) != 0)
435
 
        _warn ("AudioRecord: Error: can't close file");
436
 
 
437
 
 
438
 
}
439
 
 
440
 
void AudioRecord::recSpkrData (SFLDataFormat* buffer, int nSamples)
441
 
{
442
 
 
443
 
    if (recordingEnabled_) {
444
 
 
445
 
        nbSamplesMic_ = nSamples;
446
 
 
447
 
        for (int i = 0; i < nbSamplesMic_; i++)
448
 
            micBuffer_[i] = buffer[i];
449
 
    }
450
 
 
451
 
    return;
452
 
}
453
 
 
454
 
 
455
 
void AudioRecord::recMicData (SFLDataFormat* buffer, int nSamples)
456
 
{
457
 
 
458
 
    if (recordingEnabled_) {
459
 
 
460
 
        nbSamplesSpk_ = nSamples;
461
 
 
462
 
        for (int i = 0; i < nbSamplesSpk_; i++)
463
 
            spkBuffer_[i] = buffer[i];
464
 
 
465
 
    }
466
 
 
467
 
    return;
468
 
}
469
 
 
470
 
 
471
 
void AudioRecord::recData (SFLDataFormat* buffer, int nSamples)
472
 
{
473
 
 
474
 
    if (recordingEnabled_) {
475
 
 
476
 
        if (fp == 0) {
477
 
            _debug ("AudioRecord: Can't record data, a file has not yet been opened!");
478
 
            return;
479
 
        }
480
 
 
481
 
 
482
 
 
483
 
        if (sndFormat_ == INT16) {   // TODO change INT16 to SINT16
484
 
            if (fwrite (buffer, sizeof (SFLDataFormat), nSamples, fp) != (unsigned int) nSamples)
485
 
                _warn ("AudioRecord: Could not record data! ");
486
 
            else {
487
 
                fflush (fp);
488
 
                byteCounter_ += (unsigned long) (nSamples*sizeof (SFLDataFormat));
489
 
            }
490
 
        }
491
 
    }
492
 
 
493
 
    return;
494
 
}
495
 
 
496
 
 
497
 
void AudioRecord::recData (SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int nSamples_1, int nSamples_2)
498
 
{
499
 
 
500
 
    if (recordingEnabled_) {
501
 
 
502
 
        if (fp == 0) {
503
 
            _debug ("AudioRecord: Can't record data, a file has not yet been opened!");
504
 
            return;
505
 
        }
506
 
 
507
 
 
508
 
        if (sndFormat_ == INT16) {   // TODO change INT16 to SINT16
509
 
            for (int k=0; k<nSamples_1; k++) {
510
 
 
511
 
                mixBuffer_[k] = (buffer_1[k]+buffer_2[k]);
512
 
 
513
 
 
514
 
                if (fwrite (&mixBuffer_[k], 2, 1, fp) != 1)
515
 
                    _warn ("AudioRecord: Could not record data!");
516
 
                else {
517
 
                    fflush (fp);
518
 
                }
519
 
            }
520
 
        }
521
 
 
522
 
        byteCounter_ += (unsigned long) (nSamples_1*sizeof (SFLDataFormat));
523
 
 
524
 
    }
525
 
 
526
 
    return;
527
 
}
528