audiorecord.h 4.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 *  Copyright (C) 2008 Savoir-Faire Linux inc.
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <iostream>
#include <string.h>
23
#include <sndfile.h>
24
25

#include "global.h"
26
#include "plug-in/plugin.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using namespace std;


// structure for the wave header
struct wavhdr {
  char riff[4];           // "RIFF"
  SINT32 file_size;       // in bytes
  char wave[4];           // "WAVE"
  char fmt[4];            // "fmt "
  SINT32 chunk_size;      // in bytes (16 for PCM)
  SINT16 format_tag;      // 1=PCM, 2=ADPCM, 3=IEEE float, 6=A-Law, 7=Mu-Law
  SINT16 num_chans;       // 1=mono, 2=stereo
  SINT32 sample_rate;
  SINT32 bytes_per_sec;
  SINT16 bytes_per_samp;  // 2=16-bit mono, 4=16-bit stereo
  SINT16 bits_per_samp;
  char data[4];           // "data"
  SINT32 data_length;     // in bytes
};

47
48
typedef std::string CallID;

49
50
51
52
53
54
55
56
57
class AudioRecord
{

public:

  AudioRecord();

  void setSndSamplingRate(int smplRate);

58
59
  void setRecordingOption(std::string name, FILE_TYPE type, SOUND_FORMAT format, int sndSmplRate);

60
  /** 
61
62
63
64
65
   * Check if no otehr file is opened, then create a new one
   * @param fileName A string containing teh file (with/without extension)
   * @param type     The sound file format (FILE_RAW, FILE_WAVE)
   * @param format   Internal sound format (INT16 / INT32)
   */
66
  void openFile();
67
68
69
70
71
72
73
74
75
76
77

  /**
   * Close the opend recording file. If wave: cout the number of byte
   */
  void closeFile();

  /**
   * Check if a file is already opened
   */
  bool isOpenFile();

78
79
80
81
82
  /** 
   * Check if a file already exist
   */
  bool isFileExist();

83
84
85
86
87
  /**
   * Check recording state 
   */ 
  bool isRecording();

alexandresavard's avatar
alexandresavard committed
88
89
90
91
92
  /**
   * Set recording flag
   */
  bool setRecording();

93
94
95
96
97
  /**
   * Stop recording flag
   */
  void stopRecording();

98
99
100
101
102
  /**
   * Record a chunk of data in an openend file
   * @param buffer  The data chunk to be recorded
   * @param nSamples Number of samples (number of bytes) to be recorded 
   */
103
104
105
106
107
108
109
110
111
112
  void recData(SFLDataFormat* buffer, int nSamples);

  /**
   * Record a chunk of data in an openend file, Mix two differnet buffer
   * @param buffer_1  The first data chunk to be recorded
   * @param buffer_2  The second data chunk to be recorded
   * @param nSamples_1 Number of samples (number of bytes) of buffer_1
   * @param nSamples_2 Number of samples (number of bytes) of buffer_2
   */
  void recData(SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int nSamples_1, int nSamples_2);
113
114
115
116
117
118

protected:

  /**
   * Set the header for raw files
   */
119
  bool setRawFile();
120
121
122
123

  /**
   * Set the header for wave files
   */
124
125
126
127
128
129
130
131
132
133
134
  bool setWavFile();

  /**
   * Open an existing raw file, used when the call is set on hold    
   */
  bool openExistingRawFile();

  /**
   * Open an existing wav file, used when the call is set on hold
   */
  bool openExistingWavFile();
135
136
137
138
139
140

  /**
   * Compute the number of byte recorded and close the file
   */
  void closeWavFile();

141
142
143
144
145
  /**
   * Given two buffers, return one mixed audio buffer
   */
  void mixBuffers(SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int nSamples_1, int nSamples_2);

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  /**
   * Pointer to the recorded file
   */
  FILE *fp;                      //file pointer

  /**
   * File format (RAW / WAVE)
   */
  FILE_TYPE fileType_;

  /**
   * Sound format (SINT16/SINT32)
   */
  SOUND_FORMAT sndFormat_;

  /**
   * Number of channels
   */
  int channels_;

  /**
alexandresavard's avatar
alexandresavard committed
167
   * Number of byte recorded
168
169
170
171
172
173
174
175
   */
  unsigned long byteCounter_;

  /**
   * Sampling rate
   */
  int sndSmplRate_;

alexandresavard's avatar
alexandresavard committed
176
177
178
179
180
  /**
   * Recording flage
   */
  bool recordingEnabled_;

181
182
183
184
  /**
   * Buffer used for mixing two channels
   */
  SFLDataFormat* mixBuffer_;
185
186
187
188
189
190
  
  /**
   * Filename for this recording
   */
  char fileName_[8192];

191

192
};