-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpcmsamplepair.cpp
More file actions
525 lines (463 loc) · 11.3 KB
/
pcmsamplepair.cpp
File metadata and controls
525 lines (463 loc) · 11.3 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "pcmsamplepair.h"
PCMSample::PCMSample()
{
this->clear();
}
PCMSample::PCMSample(const PCMSample &in_object)
{
audio_word = in_object.audio_word;
index = in_object.index;
data_block_ok = in_object.data_block_ok;
word_valid = in_object.word_valid;
word_fixed = in_object.word_fixed;
word_masked = in_object.word_masked;
}
PCMSample& PCMSample::operator= (const PCMSample &in_object)
{
if(this==&in_object) return *this;
audio_word = in_object.audio_word;
index = in_object.index;
data_block_ok = in_object.data_block_ok;
word_valid = in_object.word_valid;
word_fixed = in_object.word_fixed;
word_masked = in_object.word_masked;
return *this;
}
//------------------------ Reset object.
void PCMSample::clear()
{
audio_word = 0;
index = 0;
data_block_ok = word_valid = word_fixed = word_masked = false;
}
//------------------------ Set value of the sample.
void PCMSample::setValue(int16_t in_val)
{
audio_word = in_val;
}
//------------------------ Set index of the sample.
void PCMSample::setIndex(uint64_t in_idx)
{
index = in_idx;
}
//------------------------ Mark sample as invalid.
void PCMSample::setInvalid()
{
word_valid = false;
}
//------------------------ Mark sample as valid.
void PCMSample::setValid()
{
word_valid = true;
}
//------------------------ Mark sample as fixed by ECC.
void PCMSample::setFixed()
{
word_fixed = true;
}
//------------------------ Mark sample as processed.
void PCMSample::setMasked()
{
word_masked = true;
}
//------------------------ Set (irreversably) word validity by data block validity.
void PCMSample::setValidityByBlock()
{
word_valid = data_block_ok;
}
//------------------------ Get value of the sample.
int16_t PCMSample::getValue()
{
return audio_word;
}
//------------------------ Get amlitude of the sample (rectify audio).
uint16_t PCMSample::getAmplitude()
{
if(audio_word<0)
{
return (uint16_t)(0-audio_word);
}
else
{
return (uint16_t)audio_word;
}
}
//------------------------ Get index of the sample;
uint64_t PCMSample::getIndex()
{
return index;
}
//------------------------ Is sample zeroed?
bool PCMSample::isSilent()
{
if(audio_word==0)
{
return true;
}
return false;
}
//------------------------ Is sample valid?
bool PCMSample::isValid()
{
return word_valid;
}
//------------------------ Was sample fixed by ECC?
bool PCMSample::isFixed()
{
return word_fixed;
}
//------------------------ Is sample processed?
bool PCMSample::isMasked()
{
return word_masked;
}
//------------------------ Convert PCM data to string for debug.
std::string PCMSample::dumpWordsString()
{
std::string text_out;
uint8_t bit_pos;
if(data_block_ok==false)
{
text_out = DUMP_WBRL_BAD;
}
else
{
text_out = DUMP_WBRL_OK;
}
// Printing 16 bit samples data.
bit_pos = 16;
do
{
bit_pos--;
if(word_valid==false)
{
if((audio_word&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO_BAD;
}
else
{
text_out += DUMP_BIT_ONE_BAD;
}
}
else
{
if((audio_word&(1<<bit_pos))==0)
{
text_out += DUMP_BIT_ZERO;
}
else
{
text_out += DUMP_BIT_ONE;
}
}
}
while(bit_pos>0);
if(data_block_ok==false)
{
text_out += DUMP_WBRR_BAD;
}
else
{
text_out += DUMP_WBRR_OK;
}
return text_out;
}
//------------------------ [PCMSample] pair container.
PCMSamplePair::PCMSamplePair()
{
this->clear();
}
PCMSamplePair::PCMSamplePair(const PCMSamplePair &in_object)
{
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
samples[idx] = in_object.samples[idx];
}
sample_rate = in_object.sample_rate;
emphasis = in_object.emphasis;
service_type = in_object.service_type;
file_path = in_object.file_path;
}
PCMSamplePair& PCMSamplePair::operator= (const PCMSamplePair &in_object)
{
if(this==&in_object) return *this;
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
samples[idx] = in_object.samples[idx];
}
sample_rate = in_object.sample_rate;
emphasis = in_object.emphasis;
service_type = in_object.service_type;
file_path = in_object.file_path;
return *this;
}
//------------------------ Reset object.
void PCMSamplePair::clear()
{
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
samples[idx].clear();
}
sample_rate = SAMPLE_RATE_44056;
emphasis = false;
service_type = SRV_NO;
file_path.clear();
}
//------------------------ Set service tag "new file opened".
void PCMSamplePair::setServNewFile(std::string path)
{
// Clear all fields.
clear();
// Set source file path.
file_path = path;
service_type = SRV_NEW_FILE;
}
//------------------------ Set service tag "file ended".
void PCMSamplePair::setServEndFile()
{
// Clear all fields.
clear();
service_type = SRV_END_FILE;
}
//------------------------ Set sample for one channel.
bool PCMSamplePair::setSample(uint8_t channel, int16_t sample, bool block_ok, bool word_ok, bool word_fixed)
{
if(channel>=CH_MAX)
{
return false;
}
samples[channel].audio_word = sample;
samples[channel].data_block_ok = block_ok;
samples[channel].word_valid = word_ok;
samples[channel].word_fixed = word_fixed;
return true;
}
//------------------------ Set sample pair.
void PCMSamplePair::setSamplePair(int16_t ch1_sample, int16_t ch2_sample,
bool ch1_block, bool ch2_block,
bool ch1_word, bool ch2_word,
bool ch1_fixed, bool ch2_fixed)
{
setSample(CH_LEFT, ch1_sample, ch1_block, ch1_word, ch1_fixed);
setSample(CH_RIGHT, ch2_sample, ch2_block, ch2_word, ch2_fixed);
}
//------------------------ Set (irreversably) samples validity by data block validity.
void PCMSamplePair::setValidityByBlock()
{
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
samples[idx].setValidityByBlock();
}
}
//------------------------ Set sample rate.
void PCMSamplePair::setSampleRate(uint16_t in_rate)
{
if(in_rate<SAMPLE_RATE_MAX)
{
sample_rate = in_rate;
}
}
//------------------------ Set index of the set.
void PCMSamplePair::setIndex(uint64_t in_idx)
{
// Set the same index for all underlying samples.
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
samples[idx].setIndex(in_idx);
}
}
//------------------------ Set emphasis state for samples.
void PCMSamplePair::setEmphasis(bool on)
{
emphasis = on;
}
//------------------------ Set sample for one channel.
int16_t PCMSamplePair::getSample(uint8_t channel)
{
if(channel>=CH_MAX)
{
return 0;
}
return samples[channel].audio_word;
}
//------------------------ Get sample rate.
uint16_t PCMSamplePair::getSampleRate()
{
return sample_rate;
}
//------------------------ Get size of the sample in bytes.
size_t PCMSamplePair::getSampleSize()
{
return sizeof(sample_rate);
}
//------------------------ Get index of the set;
uint64_t PCMSamplePair::getIndex()
{
uint64_t index;
// Assume that all underlying indexes are the same and pick first one.
index = samples[0].getIndex();
#ifdef QT_VERSION
for(uint8_t idx=1;idx<CH_MAX;idx++)
{
if(samples[idx].getIndex()!=index)
{
qWarning()<<DBG_ANCHOR<<"Samples indexes mismatch!"<<index<<samples[idx].getIndex();
}
}
#endif
return index;
}
//------------------------ Get state of the exact sample.
bool PCMSamplePair::isSampleValid(uint8_t channel)
{
if(channel>=CH_MAX)
{
return false;
}
return samples[channel].word_valid;
}
//------------------------ Get state of source word.
bool PCMSamplePair::isBlockValid(uint8_t channel)
{
if(channel>=CH_MAX)
{
return false;
}
return samples[channel].data_block_ok;
}
//------------------------ Should de-emphasis be applied?
bool PCMSamplePair::isEmphasisSet()
{
return emphasis;
}
//------------------------ Are all samples zeroed?
bool PCMSamplePair::isSilent()
{
bool silent;
silent = true;
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
silent = silent&&samples[idx].isSilent();
}
return silent;
}
//------------------------ Check if it has any service tag.
bool PCMSamplePair::isServicePair()
{
if(service_type==SRV_NO)
{
return false;
}
return true;
}
//------------------------ Check if it has service tag "new file opened".
bool PCMSamplePair::isServNewFile()
{
if(service_type==SRV_NEW_FILE)
{
return true;
}
return false;
}
//------------------------ Check if is has service tag "file ended".
bool PCMSamplePair::isServEndFile()
{
if(service_type==SRV_END_FILE)
{
return true;
}
return false;
}
//------------------------ Are samples ready for output (valid or processed)?
bool PCMSamplePair::isReadyForOutput()
{
bool ready;
ready = true;
for(uint8_t idx=0;idx<CH_MAX;idx++) // TODO: set to all channels
//for(uint8_t idx=0;idx<CH_RIGHT;idx++)
{
ready = ready&&(samples[idx].isValid()||samples[idx].isMasked());
if(ready==false) break;
}
return ready;
}
//------------------------ Convert PCM data to string for debug.
std::string PCMSamplePair::dumpWordsString()
{
std::string text_out;
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
text_out += samples[idx].dumpWordsString();
}
return text_out;
}
//------------------------ Output full information about the sample pair.
std::string PCMSamplePair::dumpContentString()
{
std::string text_out;
char c_buf[256];
if(isServicePair()!=false)
{
if(isServNewFile()!=false)
{
sprintf(c_buf, "SERVICE PAIR: next samples are from new file: %s", file_path.substr(0, 192).c_str());
text_out += c_buf;
}
else if(isServEndFile()!=false)
{
text_out += "SERVICE PAIR: previous samples were last in the file";
}
}
else
{
sprintf(c_buf, "I[%08u]", (uint32_t)getIndex());
text_out = c_buf;
text_out += dumpWordsString();
text_out += " F[";
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
if(samples[idx].isFixed()==false)
{
text_out += " ";
}
else
{
text_out += "+";
}
if(idx!=(CH_MAX-1))
{
text_out += "|";
}
}
text_out += "]";
text_out += " M[";
for(uint8_t idx=0;idx<CH_MAX;idx++)
{
if(samples[idx].isMasked()==false)
{
text_out += " ";
}
else
{
text_out += "+";
}
if(idx!=(CH_MAX-1))
{
text_out += "|";
}
}
text_out += "]";
sprintf(c_buf, " S[%05u]", sample_rate);
text_out += c_buf;
if(isEmphasisSet()==false)
{
text_out += " NOEMPH ";
}
else
{
text_out += " EMPHASIS";
}
}
return text_out;
}