summaryrefslogtreecommitdiff
path: root/noncore/multimedia/opierec/wavFile.cpp
blob: a0423f0ac9311469346455d1c395766b6a535d1c (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//wavFile.cpp
#include "wavFile.h"
#include "qtrec.h"

/* OPIE */
#include <opie2/odebug.h>
#include <qpe/config.h>
using namespace Opie::Core;

/* QT */
#include <qmessagebox.h>
#include <qdir.h>

/* STD */
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <fcntl.h>
#include <math.h>
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

WavFile::WavFile( QObject * parent,const QString &fileName, bool makeNwFile, int sampleRate,
                  int channels, int resolution, int format )
        : QObject( parent)
{
		owarn << "new wave file: " << fileName << oendl;
    bool b =  makeNwFile;
    wavSampleRate=sampleRate;
    wavFormat=format;
    wavChannels=channels;
    wavResolution=resolution;
    useTmpFile=false;
    if( b) {
        newFile();
    } else {
        openFile(fileName);
    }
}

bool WavFile::newFile() {

//  odebug << "Set up new file" << oendl;
  Config cfg("OpieRec");
  cfg.setGroup("Settings");

  currentFileName=cfg.readEntry("directory",QDir::homeDirPath());
  QString date;
  QDateTime dt = QDateTime::currentDateTime();
  date = dt.toString();//TimeString::dateString( QDateTime::currentDateTime(),false,true);
  date.replace(QRegExp("'"),"");
  date.replace(QRegExp(" "),"_");
  date.replace(QRegExp(":"),"-");
  date.replace(QRegExp(","),"");

  QString currentFile=date;
  if(currentFileName.right(1).find("/",0,true) == -1)
    currentFileName += "/" + date;
  else
    currentFileName += date;
  currentFileName+=".wav";

//  odebug << "set up file for recording: "+currentFileName << oendl;
  char pointer[] = "/tmp/opierec-XXXXXX";
  int fd = 0;

  if( currentFileName.find("/mnt",0,true) == -1
      && currentFileName.find("/tmp",0,true) == -1 ) {
    // if destination file is most likely in flash (assuming jffs2)
    // we have to write to a different filesystem first

    useTmpFile = true;
    if(( fd = mkstemp( pointer)) < 0 ) {
       perror("mkstemp failed");
       return false;
    }

//    odebug << "Opening tmp file " << pointer << "" << oendl;
    track.setName( pointer);

  } else { //just use regular file.. no moving

    useTmpFile = false;
    track.setName( currentFileName);
  }
  if(!track.open( IO_ReadWrite | IO_Truncate)) {
    QString errorMsg=(QString)strerror(errno);
    odebug << errorMsg << oendl;
    QMessageBox::message("Note", "Error opening file.\n" +errorMsg);

    return false;
  } else {
    setWavHeader( track.handle() , &hdr);
  }
return true;
}

WavFile::~WavFile() {

    closeFile();
}

void WavFile::closeFile() {
    if(track.isOpen())
        track.close();
}

int WavFile::openFile(const QString &currentFileName) {
		qWarning("open play file "+currentFileName);;
    closeFile();

		track.setName(currentFileName);

		if(!track.open(IO_ReadOnly)) {
				QString errorMsg=(QString)strerror(errno);
				odebug << "<<<<<<<<<<< "+errorMsg+currentFileName << oendl;
				QMessageBox::message("Note", "Error opening file.\n" +errorMsg);
				return -1;
		} else {
				parseWavHeader( track.handle());
		}
		return track.handle();
}

bool WavFile::setWavHeader(int fd, wavhdr *hdr) {

  strncpy((*hdr).riffID, "RIFF", 4); // RIFF
  strncpy((*hdr).wavID, "WAVE", 4); //WAVE
  strncpy((*hdr).fmtID, "fmt ", 4); // fmt
  (*hdr).fmtLen = 16; // format length = 16

  if( wavFormat == WAVE_FORMAT_PCM) {
    (*hdr).fmtTag = 1; // PCM
//    odebug << "set header  WAVE_FORMAT_PCM" << oendl;
  }
  else {
    (*hdr).fmtTag = WAVE_FORMAT_DVI_ADPCM; //intel ADPCM
 //    odebug << "set header  WAVE_FORMAT_DVI_ADPCM" << oendl;
  }

  //  (*hdr).nChannels = 1;//filePara.channels;// ? 2 : 1*/; // channels
  (*hdr).nChannels = wavChannels;// ? 2 : 1*/; // channels

  (*hdr).sampleRate = wavSampleRate; //samples per second
  (*hdr).avgBytesPerSec = (wavSampleRate)*( wavChannels*(wavResolution/8)); // bytes per second
  (*hdr).nBlockAlign = wavChannels*( wavResolution/8); //block align
  (*hdr).bitsPerSample = wavResolution; //bits per sample 8, or 16

  strncpy((*hdr).dataID, "data", 4);

  write( fd,hdr, sizeof(*hdr));
//   owarn << "writing header: bitrate " << wavResolution << ", samplerate " << wavSampleRate << ",  channels " << wavChannels << oendl;
  return true;
}

bool WavFile::adjustHeaders(int fd, int total) {
  lseek(fd, 4, SEEK_SET);
  int i = total + 36;
  write( fd, &i, sizeof(i));
  lseek( fd, 40, SEEK_SET);
  write( fd, &total, sizeof(total));
//  owarn << "adjusting header " << total << "" << oendl;
  return true;
}

int WavFile::parseWavHeader(int fd) {
//  owarn << "Parsing wav header" << oendl;
  char string[4];
  int found;
  short fmt;
  unsigned short ch, bitrate;
  unsigned long samplerrate, longdata;

  if (read(fd, string, 4) < 4) {
//    owarn << " Could not read from sound file." << oendl;
    return -1;
  }
  if (strncmp(string, "RIFF", 4)) {
//    owarn << " not a valid WAV file." << oendl;
    return -1;
  }
  lseek(fd, 4, SEEK_CUR);
  if (read(fd, string, 4) < 4) {
//    owarn << "Could not read from sound file." << oendl;
    return -1;
  }
  if (strncmp(string, "WAVE", 4)) {
//    owarn << "not a valid WAV file." << oendl;
    return -1;
  }
  found = 0;

  while (!found) {
    if (read(fd, string, 4) < 4) {
//      owarn << "Could not read from sound file." << oendl;
      return -1;
    }
    if (strncmp(string, "fmt ", 4)) {
      if (read(fd, &longdata, 4) < 4) {
//        owarn << "Could not read from sound file." << oendl;
        return -1;
      }
      lseek(fd, longdata, SEEK_CUR);
    } else {
      lseek(fd, 4, SEEK_CUR);
      if (read(fd, &fmt, 2) < 2) {
//        owarn << "Could not read format chunk." << oendl;
        return -1;
      }
      if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) {
//        owarn << "Wave file contains unknown format. Unable to continue." << oendl;
        return -1;
      }
      wavFormat = fmt;
      //      compressionFormat=fmt;
//      owarn << "compressionFormat is " << fmt << "" << oendl;
      if (read(fd, &ch, 2) < 2) {
//        owarn << "Could not read format chunk." << oendl;
        return -1;
      } else {
        wavChannels = ch;
//        owarn << "File has " << ch << " channels" << oendl;
      }
      if (read(fd, &samplerrate, 4) < 4) {
//        owarn << "Could not read from format chunk." << oendl;
        return -1;
      } else {
        wavSampleRate = samplerrate;
        //                sampleRate = samplerrate;
//        owarn << "File has samplerate of " << (int) samplerrate << "" << oendl;
      }
      lseek(fd, 6, SEEK_CUR);
      if (read(fd, &bitrate, 2) < 2) {
//        owarn << "Could not read format chunk." << oendl;
        return -1;
      }  else {
        wavResolution=bitrate;
        //                 resolution =  bitrate;
//        owarn << "File has bitrate of " << bitrate << "" << oendl;
      }
      found++;
    }
  }
  found = 0;
  while (!found) {
    if (read(fd, string, 4) < 4) {
      odebug << "Could not read from sound file." << oendl;
      return -1;
    }

    if (strncmp(string, "data", 4)) {
      if (read(fd, &longdata, 4)<4) {
        odebug << "Could not read from sound file." << oendl;
        return -1;
      }

      lseek(fd, longdata, SEEK_CUR);
    } else {
      if (read(fd, &longdata, 4) < 4) {
        odebug << "Could not read from sound file." << oendl;
        return -1;
      } else {
        wavNumberSamples =  longdata;
         odebug << "file hase length of " << (int)longdata << ""
                << "lasting "
                << (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8))
                << " seconds" << oendl;
//        wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8));

  return longdata;
      }
    }
  }

  lseek(fd, 0, SEEK_SET);

  return 0;
}

QString WavFile::trackName() {
    return track.name();
}

int WavFile::wavHandle(){
  return track.handle();
}

int WavFile::getFormat() {
return wavFormat;
}

int WavFile::getResolution() {
return wavResolution;
}

int WavFile::getSampleRate() {
 return wavSampleRate;
}

int WavFile::getNumberSamples() {
 return wavNumberSamples;
}

bool WavFile::isTempFile() {
return useTmpFile;
}

int WavFile::getChannels() {

   return wavChannels;
}