audiorecord.h 5.38 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *  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.
18
19
20
21
22
23
24
25
26
27
28
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
29
30
 */

31
32
#ifndef _AUDIO_RECORD_H
#define _AUDIO_RECORD_H
33

34
35
#include <string>
#include <cstdlib>
36
#include <memory>
37

38
#include "sfl_types.h"
39
#include "noncopyable.h"
40

41
class AudioRecord {
42

Julien Bonjean's avatar
Julien Bonjean committed
43
    public:
44
        enum FILE_TYPE { FILE_RAW, FILE_WAV, FILE_INVALID };
Julien Bonjean's avatar
Julien Bonjean committed
45
46
47

        AudioRecord();

48
49
        void setSndSamplingRate(int smplRate);
        void setRecordingOption(FILE_TYPE type, int sndSmplRate, const std::string &path);
Julien Bonjean's avatar
Julien Bonjean committed
50

51
52
53
        /**
         * Init recording file path
         */
54
        void initFilename(const std::string &peerNumber);
Julien Bonjean's avatar
Julien Bonjean committed
55

56
        /**
57
58
        	 * Return the filepath of the recording
        	 */
59
        std::string getFilename() const;
60

Julien Bonjean's avatar
Julien Bonjean committed
61
62
        /**
         * Check if no otehr file is opened, then create a new one
63
         * @param filename A string containing teh file (with/without extension)
Julien Bonjean's avatar
Julien Bonjean committed
64
65
         * @param type     The sound file format (FILE_RAW, FILE_WAVE)
         * @param format   Internal sound format (INT16 / INT32)
66
         * @return bool    True if file was opened
Julien Bonjean's avatar
Julien Bonjean committed
67
         */
68
        bool openFile();
Julien Bonjean's avatar
Julien Bonjean committed
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
         */
78
        bool isOpenFile() const;
Julien Bonjean's avatar
Julien Bonjean committed
79
80

        /**
81
         * Check if a file already exists
Julien Bonjean's avatar
Julien Bonjean committed
82
         */
83
        bool fileExists() const;
Julien Bonjean's avatar
Julien Bonjean committed
84
85
86
87

        /**
         * Check recording state
         */
88
        bool isRecording() const;
Julien Bonjean's avatar
Julien Bonjean committed
89
90
91
92

        /**
         * Set recording flag
         */
93
        void setRecording();
Julien Bonjean's avatar
Julien Bonjean committed
94
95
96
97
98
99
100
101
102
103
104

        /**
         * Stop recording flag
         */
        void stopRecording();

        /**
         * 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
         */
105
        void recData(SFLDataFormat* buffer, size_t nSamples);
Julien Bonjean's avatar
Julien Bonjean committed
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

    protected:

        /**
         * Create name file according to current date
         */
        void createFilename();

        /**
         * Set the header for raw files
         */
        bool setRawFile();

        /**
         * Set the header for wave files
         */
        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();

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


        /**
         * Pointer to the recorded file
         */
143
        FILE *fileHandle_;
Julien Bonjean's avatar
Julien Bonjean committed
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

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

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

        /**
         * Number of byte recorded
         */
        unsigned long byteCounter_;

        /**
         * Sampling rate
         */
        int sndSmplRate_;

        /**
         * number of samples recorded for mic buffer
         */
        int nbSamplesMic_;

        /**
         * number of samples recorded for speaker buffer
         */
        int nbSamplesSpk_;

        /**
         * Maximum number of samples
         */
178
        static const int NB_SAMPLES_MAX = 3000;
Julien Bonjean's avatar
Julien Bonjean committed
179
180
181
182
183
184
185
186
187

        /**
         * Recording flage
         */
        bool recordingEnabled_;

        /**
         * Buffer used for mixing two channels
         */
188
        SFLDataFormat mixBuffer_[NB_SAMPLES_MAX];
Julien Bonjean's avatar
Julien Bonjean committed
189
190
191
192

        /**
         * Buffer used to copy mic info
         */
193
        SFLDataFormat micBuffer_[NB_SAMPLES_MAX];
Julien Bonjean's avatar
Julien Bonjean committed
194
195
196
197

        /**
         * Buffer used to copy spkr info
         */
198
        SFLDataFormat spkBuffer_[NB_SAMPLES_MAX];
Julien Bonjean's avatar
Julien Bonjean committed
199
200
201
202

        /**
         * Filename for this recording
         */
203
        std::string filename_;
Julien Bonjean's avatar
Julien Bonjean committed
204
205
206
207
208
209

        /**
         * Path for this recording
         */
        std::string savePath_;

210
211
    private:
        NON_COPYABLE(AudioRecord);
212
};
213
214

#endif // _AUDIO_RECORD_H