File: qwav.cc

package info (click to toggle)
quelcom 0.4.0-16
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 1,544 kB
  • sloc: cpp: 4,148; makefile: 158; sh: 15
file content (311 lines) | stat: -rw-r--r-- 8,235 bytes parent folder | download | duplicates (3)
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
/*! \file
 * implementation functions for class qwav
 */

#include <iostream>

# include "qwav.hh"
# include "qexception.hh"

#ifdef NLS
  # include <locale.h>
  # include <libintl.h>
  # define _(s) gettext (s)
#else
  # define _(s) (s)
#endif


u_int32_t qwav::append (qwav w) {

  if (!compatible(w))
    throw qexception (__PRETTY_FUNCTION__,
                      getName() + string(": ") + w.getName() +
                      string(_(" is not compatible")));
  
  qfile::append(w.getMap()+HEADERSIZE,w.getSize()-HEADERSIZE);

  remap(getMap());
  addSamples(w.getSamples());

  return getSamples();
}


void qwav::print (ostream *os) {

  u_int32_t msduration;

  msduration = getMsDuration();

 // cal posar les milssimes?
  *os << getName() << ": " << getSampleRate() << " Hz  "
      << getBitsPerSample() << " bits  "
      << ((getChannels()==1)?"mono":"stereo") << ' ';
  os->width(2);
  *os << msduration/60000 << ':';
  os->width(2);
  os->fill('0');
  *os << (msduration/1000)%60 << '.';
  os->width(2);
  os->fill('0');
  *os << (msduration%1000)/10;
  /*
  os->form("%s: %u Hz  %u bits  %s  %2u:%02u.%02u",
	   getName().c_str(),
           getSampleRate(),getBitsPerSample(),
           ((getChannels()==1)?"mono":"stereo"),
           msduration/60000,(msduration/1000)%60,(msduration%1000)/10);
  */
}


qwav::qwav(string name, u_int32_t flags) 
     : qfile (name,flags), 
       qwavheader(getMap(),getSize()) { 

}


bool qwav::compatible (qwav &w) {

  return (getStereo() == w.getStereo())    &&
       (getChannels() == w.getChannels())  &&
     (getSampleRate() == w.getSampleRate());
}


u_int32_t qwav::cut (u_int32_t firstsample, u_int32_t lastsample) {

 // s'hauria de comprovar la consistncia dels parmetres...

  qfile::cut(getOffset(firstsample),getOffset(lastsample)+getBytesPerSample()-1);

  remap(getMap());
  setSamples((getSize()-qwavheader::HEADERSIZE)/getBytesPerSample());

  return getSamples();
}


void qwav::move (u_int32_t sample_start, u_int32_t sample_end, u_int32_t sample_dest) {

  if (sample_end<sample_start) {
    return; 	// ...
  }

  if (sample_start>getSamples()) {
    return; 	// ...
  }

  if (sample_end>getSamples()) {
    sample_end = getSamples();	// ...
  }

 // revisar d'ac en avall...
  u_int32_t sample_number = sample_end-sample_start;
  
  if (sample_number+sample_dest-1>getSamples()) {
    return;	// ...
  }

  qfile::move(getOffset(sample_start),
              getOffset(sample_end)+getBytesPerSample()-1,
              getOffset(sample_dest));
}


void qwav::truncate (u_int32_t samples) {

  if (samples>=getSamples())
    return;

  qfile::truncate(HEADERSIZE+samples*getBytesPerSample());

  remap(getMap());

  setSamples(samples);
}


u_int32_t qwav::cut (qcuthandler &h) {

  if ((!h.getDel()) && (h.getOutfile()==""))
    return 0;	
   
  //
  // an schema with the cut specifications and what will be done
  //  B => begin point, from the beginning of the file
  //  E => end point, from the beginning of the file
  //  b => begin point, from the end of the file
  //  e => end point, from the end of the file
  //  s => size of the fragment
  // 
  //  0 means not specified 	1 means specified
  //
  //   B b E e s
  //   0 0 0 0 0	throw
  //   0 0 0 0 1	0 .. s
  //   0 0 0 1 0	0 .. e
  //   0 0 0 1 1	e-s .. e
  //   0 0 1 0 0	0 .. E
  //   0 0 1 0 1	E-s .. E
  //   0 0 1 1 0	throw
  //   0 0 1 1 1	throw
  //   0 1 0 0 0	b .. EOF
  //   0 1 0 0 1	b .. b+s
  //   0 1 0 1 0	b .. e
  //   0 1 0 1 1	throw
  //   0 1 1 0 0	b .. E
  //   0 1 1 0 1	throw
  //   0 1 1 1 0	throw
  //   0 1 1 1 1	throw
  //   1 0 0 0 0	B .. EOF
  //   1 0 0 0 1	B .. B+s
  //   1 0 0 1 0	B .. e
  //   1 0 0 1 1	throw
  //   1 0 1 0 0	B .. E
  //   1 0 1 0 1	throw
  //   1 0 1 1 0	throw
  //   1 0 1 1 1	throw
  //   1 1 0 0 0	throw
  //   1 1 0 0 1	throw
  //   1 1 0 1 0	throw
  //   1 1 0 1 1	throw
  //   1 1 1 0 0	throw
  //   1 1 1 0 1	throw
  //   1 1 1 1 0	throw
  //   1 1 1 1 1	throw

  // it is ugly, actually, but the table above should make things clear
  
  u_int32_t cutmode=0;
  if ( h.getBegin().getFormat()!=qvf::UNDEFINED ) cutmode+=16;
  if ( h.getbegin().getFormat()!=qvf::UNDEFINED ) cutmode+=8;
  if (   h.getEnd().getFormat()!=qvf::UNDEFINED ) cutmode+=4;
  if (   h.getend().getFormat()!=qvf::UNDEFINED ) cutmode+=2;
  if (  h.getSize().getFormat()!=qvf::UNDEFINED ) cutmode+=1;

  u_int32_t first_sample, last_sample;

#ifdef QVERBOSE
  cerr << "cut mode: " << cutmode << endl;
#endif

 // comprovaci de zeros: -s 3b => -s 0!!
  switch (cutmode) {
    case 0:
      //cerr << "cut: you must specify where to cut" << endl;
      return 0;
    case 1: 	// 1 .. s		(p)
      first_sample = 1;
      last_sample = getSample(h.getSize());
    break;
    case 2:	// 1 .. e		(p)
      first_sample = 1;
      last_sample = getSamples()+1-getSample(h.getend());
    break;
    case 3:	// e-s .. e		(p)
      last_sample = getSamples()+1-getSample(h.getend());
      first_sample = last_sample-getSample(h.getSize())+1;
    break;
    case 4:	// 1 .. E		(p)
      first_sample = 1;
      last_sample = getSample(h.getEnd());
    break;
    case 5:	// E-s .. E		(p)
      last_sample = getSample(h.getEnd());
      first_sample = last_sample-getSample(h.getSize())+1;
    break;
    case 6:	// E+e => throw		(p)
    case 7:	// E+e+s => throw	(p)
    case 14:	// b+E+e => throw	(p)
    case 15:	// b+E+e+s => throw	(p)
    case 22:	// B+E+e => throw	(p)
    case 23:	// B+E+e+s => throw	(p)
      throw qexception(__PRETTY_FUNCTION__,string(_("options -E and -e cannot be used together")));
    case 8:	// b .. EOF		(p)
      first_sample = getSamples()+1-getSample(h.getbegin());
      last_sample = getSamples();
    break;
    case 9:	// b .. b+s		(p)
      first_sample = getSamples()+1-getSample(h.getbegin());
      last_sample = first_sample+getSample(h.getSize())-1;
    break;
    case 10:	// b .. e		(p)
      first_sample = getSamples()+1-getSample(h.getbegin());
      last_sample = getSamples()+1-getSample(h.getend());
    break;
    case 11:	// b+e+s
    case 13:	// b+E+s
    case 19:	// B+e+s
    case 21:	// B+E+s
      throw qexception(__PRETTY_FUNCTION__,string(_("options (-b or -B), (-e or -E) and -s cannot be used together")));
    case 12:	// b .. E		(p)
      first_sample = getSamples()+1-getSample(h.getbegin());
      last_sample = getSample(h.getEnd());
    break;
    case 16:	// B .. EOF		(p)
      first_sample = getSample(h.getBegin());
      last_sample = getSamples();
    break;
    case 17:	// B .. B+s		(p)
      first_sample = getSample(h.getBegin());
      last_sample = first_sample+getSample(h.getSize())-1;
    break;
    case 18:	// B .. e		(p)
      first_sample = getSample(h.getBegin());
      last_sample = getSamples()+1-getSample(h.getend());
    break;
    case 20:	// B .. E
      first_sample = getSample(h.getBegin());
      last_sample = getSample(h.getEnd());
    break;
    case 24 ... 31: // B+b+...
      throw qexception(__PRETTY_FUNCTION__,string(_("options -B and -b cannot be used together")));
    default: 	// this should never happen
      cerr << "quelcom panic!" << endl;
      return 0;
  }

#ifdef QVERBOSE
  cerr << "cut: " << last_sample-first_sample+1 << " samples(" << first_sample << ".." << last_sample << ") => ";
#endif

  if (first_sample>last_sample)
    throw qexception(__PRETTY_FUNCTION__,
                     string(_("cut: last sample > first sample")));
// especificar quines mostres sn

  if (last_sample>getSamples())
    last_sample = getSamples();

  if (h.getOutfile()!="") 
    getWav(h.getOutfile(),first_sample,last_sample);

  if (h.getDel())
    cut(first_sample,last_sample);

  return 1;
}


qwav& qwav::getWav(string name, u_int32_t startsample, u_int32_t endsample) {

 // s'ha de poder resoldre ms elegantment...: un constructor nul de wav (nom de fitxer + parmetres i cap mostra) + un append
  qfile newwav(name,NEW);

  newwav.append(getMap(),qwavheader::HEADERSIZE);
  newwav.append(getMap()+getOffset(startsample),(endsample-startsample+1)*getBytesPerSample());

  qwavheader h(newwav.getMap());
  h.setSamples(endsample-startsample+1);

 // return ???
}


ostream &operator<<(ostream &os, qwav &u) {
  u.print(&os);
  return os;
}