audiorecord.cpp 4.24 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
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
/*
 *  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 "audiorecord.h"

AudioRecord::AudioRecord(){
  sndSmplRate_ = 44100;
  channels_ = 1;
  byteCounter_ = 0;

}


void AudioRecord::setSndSamplingRate(int smplRate){
  sndSmplRate_ = smplRate;
}


void AudioRecord::openFile(std::string fileName, FILE_TYPE type, SOUND_FORMAT format) {

  channels_ =1;
  fileType_ = type;
  byteCounter_ = 0;
  sndFormat_ = format;

  bool result = false;

  if(fileType_ == FILE_RAW){
    result = setRawFile( fileName.c_str() );
  }
  else if (fileType_ == FILE_WAV){
    result = setWavFile( fileName.c_str() );
  }

}


void AudioRecord::closeFile() {

  if (fp == 0) return;

  if (fileType_ == FILE_RAW)
    fclose(fp);
  else if (fileType_ == FILE_WAV)
    this->closeWavFile();

}


bool AudioRecord::isOpenFile() {
  
  if(fp)
    return true;
  else
    return false;
}


bool AudioRecord::setRawFile(const char *fileName) {

  char name[8192];
  strncpy(name, fileName, 8192);
  if ( strstr(name, ".raw") == NULL) strcat(name, ".raw");
  fp = fopen(name, "wb");
  if ( !fp ) {
    cout << "AudioRecord: could not create RAW file: " << name << '.';
    return false;
  }

  if ( sndFormat_ != INT16 ) { // TODO need to change INT16 to SINT16
    sndFormat_ = INT16;
    cout << "AudioRecord: using 16-bit signed integer data format for file " << name << '.';
  }

  cout << "AudioRecord: creating RAW file: " << name;
  return true;
}


bool AudioRecord::setWavFile(const char *fileName) {
  
  char name[8192];
  strncpy(name, fileName, 8192);
  if ( strstr(name, ".wav") == NULL) strcat(name, ".wav");
  fp = fopen(name, "wb");
  if ( !fp ) {
    cout << "AudioRecord: could not create WAV file: " << name;
    return false;
  }

  struct wavhdr hdr = {"RIF", 44, "WAV", "fmt", 16, 1, 1,
                        44100, 0, 2, 16, "dat", 0};
  hdr.riff[3] = 'F';
  hdr.wave[3] = 'E';
  hdr.fmt[3]  = ' ';
  hdr.data[3] = 'a';
  hdr.num_chans = channels_;
  if ( sndFormat_ == INT16 ) { //  TODO need to write INT16 to SINT16
    hdr.bits_per_samp = 16;
  }
  hdr.bytes_per_samp = (SINT16) (channels_ * hdr.bits_per_samp / 8);
  hdr.bytes_per_sec = (SINT32) (hdr.sample_rate * hdr.bytes_per_samp);


  if ( fwrite(&hdr, 4, 11, fp) != 11 ) {
    cout << "AudioRecord: could not write WAV header for file " << name << '.';
    return false;
  }

  cout << "AudioRecord: creating WAV file: " << name;
  return true;
}


void AudioRecord::closeWavFile()
{
  int bytes_per_sample = 1;
  if ( sndFormat_ == INT16 )
    bytes_per_sample = 2;


  SINT32 bytes = byteCounter_ * channels_ * bytes_per_sample;
  fseek(fp, 40, SEEK_SET); // jump to data length
  fwrite(&bytes, 4, 1, fp);

  bytes = byteCounter_ * channels_ * bytes_per_sample + 44; // + 44 for the wave header
  fseek(fp, 4, SEEK_SET); // jump to file size
  fwrite(&bytes, 4, 1, fp);
  fclose( fp );
}


void AudioRecord::recData(SFLDataFormat* buffer, int nSamples) {

  if (fp == 0){
    cout << "AudioRecord: Can't record data, a file has not yet been opened!";
    return;
  }
 
  if ( sndFormat_ == INT16 ) { // TODO change INT16 to SINT16
    if (nSamples <= 1){
      if ( fwrite(buffer, 2, 1, fp) != 1)
        cout << "AudioRecord: Could not record data!";
    }
    else { 
      for ( int k=0; k<nSamples; k++ ) {
        cout << "Buffer[" << k << "] : " << buffer[k] << "\n";
        if ( fwrite(&buffer[k], 2, 1, fp) != 1 )
          cout << "AudioRecord: Could not record data!";
      }
    }
  }

  


  byteCounter_ += (unsigned long)(sizeof(buffer) / sizeof(SINT16));

  return;
}