-
Notifications
You must be signed in to change notification settings - Fork 0
/
swavgen.c
830 lines (721 loc) · 33.1 KB
/
swavgen.c
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#include "swavgen.h"
void set_defaults(wave_prop_t* wave_prop) {
strcpy(wave_prop->file_name, "wave.wav");
wave_prop->a = 1.0f;
wave_prop->duration = 2.0; // seconds
wave_prop->f_s = 48000; // sample rate
wave_prop->f = 800; // sine wave frequency
wave_prop->p = 1.0f/wave_prop->f; // sine wave period
wave_prop->channels = 1;
wave_prop->total_number_of_samples = wave_prop->f_s * wave_prop->duration * wave_prop->channels;
wave_prop->bytes_per_sample = 8; // default to 64-bit samples
wave_prop->representation = 's'; // default to signed representation
wave_prop->type = 's'; // sine wave
strcpy(wave_prop->typestr, "sine"); // sine
wave_prop->encoding = WAVE_FORMAT_IEEE_FLOAT; // IEEE float
strcpy(wave_prop->encodingstr, "IEEE-float"); // IEEE-float
wave_prop->padding = 0;
wave_prop->valid_bits = 8 * wave_prop->bytes_per_sample;
wave_prop->channel_mask = 0;
wave_prop->extensible = 0;
}
int get_options(int* argc, char** argv, wave_prop_t* wave_prop) {
unsigned long lval = 0;
unsigned long long llval = 0;
float fval = 0;
int ival;
char strval[MAX_FILE_NAME];
for (int i = 1; i < *argc; i++) {
if (argv[i][0] != '-' && argv[i - 1][0] != '-') {
CHECK_RES(sscanf(argv[i], "%s", strval));
CHECK_ERR(get_wave_type(argv[i], wave_prop));
continue;
}
if (!(strcmp("-e", argv[i])) || !(strcmp("--encoding", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%s", strval));
CHECK_ERR(get_encoding(strval, wave_prop));
continue;
}
if (!(strcmp("-f", argv[i])) || !(strcmp("--frequency", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%ld", &lval));
CHECK_LIMITS_LONG(lval);
wave_prop->f = lval;
wave_prop->p = 1.0f/lval;
continue;
}
if (!(strcmp("-p", argv[i])) || !(strcmp("--period", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%f", &fval));
CHECK_LIMITS_FLOAT(fval);
wave_prop->p = fval;
wave_prop->f = 1/fval;
continue;
}
if (!(strcmp("-s", argv[i])) || !(strcmp("--sampling-frequency", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%ld", &lval));
CHECK_LIMITS_LONG(lval);
wave_prop->f_s = lval;
wave_prop->total_number_of_samples = wave_prop->f_s * wave_prop->duration * wave_prop->channels;
continue;
}
if (!(strcmp("-d", argv[i])) || !(strcmp("--duration", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%f", &fval));
CHECK_LIMITS_FLOAT(fval);
wave_prop->duration = fval;
wave_prop->total_number_of_samples = wave_prop->f_s * wave_prop->duration * wave_prop->channels;
continue;
}
if (!(strcmp("-n", argv[i])) || !(strcmp("--total-samples", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%lld", &llval));
CHECK_LIMITS_LONG(llval);
wave_prop->total_number_of_samples = llval;
continue;
}
if (!(strcmp("-a", argv[i])) || !(strcmp("--amplitude", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%f", &fval));
CHECK_LIMITS_FLOAT(fval);
wave_prop->a = fval;
continue;
}
if (!(strcmp("-l", argv[i])) || !(strcmp("--sample-length", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%d", &ival));
wave_prop->bytes_per_sample = ival / 8; // variable is called bytes but it's inputted as bits.
wave_prop->valid_bits = ival;
continue;
}
if (!(strcmp("-r", argv[i])) || !(strcmp("--representation", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%s", strval));
CHECK_ERR(get_represenation(strval, wave_prop));
continue;
}
if (!(strcmp("-c", argv[i])) || !(strcmp("--channels", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%d", &ival));
CHECK_LIMITS_INT(ival);
wave_prop->channels = ival;
continue;
}
if (!(strcmp("-o", argv[i])) || !(strcmp("--output", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%s", strval));
strcpy(wave_prop->file_name, strval);
continue;
}
if (!(strcmp("-x", argv[i])) || !(strcmp("--extensible",argv[i]))) {
wave_prop->extensible = 1;
continue;
}
if (!(strcmp("-v", argv[i])) || !(strcmp("--valid-samples", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%d", &ival));
CHECK_LIMITS_INT(ival);
wave_prop->valid_bits = ival;
continue;
}
if (!(strcmp("-m", argv[i])) || !(strcmp("--channel-mask", argv[i]))) {
CHECK_RES(sscanf(argv[i + 1], "%ld", &lval));
CHECK_LIMITS_LONG(lval);
wave_prop->channel_mask = lval;
continue;
}
}
/* Need to check if valid bits are not more than 8 * bytes_per_sample
* Also need to check if settings are used that require the extenisble option
* */
if (wave_prop->valid_bits > wave_prop->bytes_per_sample * 8) {
fprintf(stderr, "Valid bits must not be more than the bits per sample.");
return 1;
}
if (wave_prop->channel_mask != 0 && wave_prop->extensible != 1) {
fprintf(stderr, "If using channel masks, the extensible format must be enabled with '-x' or '--extensible'.");
return 1;
}
return 0;
}
int check_encoding_bytes(wave_prop_t* wave_prop) {
if (wave_prop->encoding == 'p') {
switch (wave_prop->bytes_per_sample) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
printf("\nSample bits must be 8, 16, 24, or 32. Please specify with '-l' or '--sample-length'.\n");
return 1;
}
}
return 0;
}
int get_wave_type(char* str, wave_prop_t* wave_prop) {
if (!(strcmp("sine", str))) {
wave_prop->type = 's';
} else if (!(strcmp("square", str))) {
wave_prop->type = 'q';
} else if (!(strcmp("triangle", str))) {
wave_prop->type = 't';
} else if (!(strcmp("saw", str))) {
wave_prop->type = 'w';
} else {
printf("\nUnknown wave type. Please enter either, 'sine', 'square', 'triangle', or 'saw'.\n");
return 1;
}
strcpy(wave_prop->typestr, str);
return 0;
}
int get_encoding(char* str, wave_prop_t* wave_prop) {
if (!(strcmp("PCM", str))) {
wave_prop->encoding = WAVE_FORMAT_PCM;
} else if (!(strcmp("IEEE-float", str))) {
wave_prop->encoding = WAVE_FORMAT_IEEE_FLOAT;
} else if (!(strcmp("A-law", str))) {
wave_prop->encoding = WAVE_FORMAT_ALAW;
} else if (!(strcmp("Mu-law", str))) {
wave_prop->encoding = WAVE_FORMAT_MULAW;
} else {
printf("\nUnknown encoding. Please enter either, 'PCM', 'IEEE-float', 'A-law', or 'Mu-law'.\n");
return 1;
}
strcpy(wave_prop->encodingstr, str);
return 0;
}
int get_represenation(char* str, wave_prop_t* wave_prop) {
if (!(strcmp("signed", str))) {
wave_prop->representation = 's';
return 0;
}
if (!(strcmp("unsigned", str))) {
wave_prop->representation = 'u';
return 0;
} else {
printf("\nUnknown binary representation. Please enter either, 'signed' or 'unsigned'.\n");
return 1;
}
}
int set_type_encoding(wave_prop_t* wave_prop) {
switch (wave_prop->type) {
case 's':
wave_prop->wave = &create_sine;
break;
case 'q':
wave_prop->wave = &create_square;
break;
case 't':
wave_prop->wave = &create_triangle;
break;
case 'w':
wave_prop->wave = &create_saw;
break;
default:
printf("\nWave type '%c' not implemented.\n", wave_prop->type);
return 1;
}
switch (wave_prop->encoding) {
case WAVE_FORMAT_PCM:
wave_prop->seth = &set_header_pcm;
wave_prop->outp = &output_pcm;
wave_prop->encd = &encode_pcm;
break;
case WAVE_FORMAT_IEEE_FLOAT:
wave_prop->seth = &set_header_ieee_float;
wave_prop->outp = &output_non_pcm;
wave_prop->encd = &encode_ieee_float;
break;
case WAVE_FORMAT_ALAW:
wave_prop->seth = &set_header_a_law;
wave_prop->outp = &output_non_pcm;
wave_prop->encd = &encode_a_law;
break;
case WAVE_FORMAT_MULAW:
wave_prop->seth = &set_header_mu_law;
wave_prop->outp = &output_non_pcm;
wave_prop->encd = &encode_mu_law;
break;
default:
printf("\nEncoding '%c' not implemented.\n", wave_prop->encoding);
return 1;
}
if (wave_prop->extensible) {
wave_prop->seth = &set_header_extensible;
wave_prop->outp = &output_extensible;
}
return 0;
}
void create_sine(double** samples, wave_prop_t* wave_prop) {
*samples = (double*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(double));
for (int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((double*)*samples)[n] = wave_prop->a * sin(2 * M_PI * wave_prop->f * n / (wave_prop->f_s * wave_prop->channels));
}
}
void create_square(double** samples, wave_prop_t* wave_prop) {
double sample;
*samples = (double*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(double));
for (int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
sample = sin(2 * M_PI * wave_prop->f * n / (wave_prop->f_s * wave_prop->channels));
((double*)*samples)[n] = wave_prop->a * (double) sgn(&sample);
}
}
void create_triangle(double** samples, wave_prop_t* wave_prop) {
*samples = (double*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(double));
/* Two implementations. Not sure which one is best. */
for (int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((double*)*samples)[n] = (2 * wave_prop->a / M_PI) * asin(sin(2 * M_PI * wave_prop->f * n / (wave_prop->f_s * wave_prop->channels)));
/* ((double*)*samples)[n] = wave_prop->a * (4 * fabs(((double)wave_prop->f * n / (wave_prop->f_s * wave_prop->channels)) - (int)(((double)wave_prop->f * n / (wave_prop->f_s * wave_prop->channels)) + 0.5f )) - 1); */
}
}
void create_saw(double** samples, wave_prop_t* wave_prop) {
*samples = (double*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(double));
/* Two implementations. Not sure which one is best. */
for (int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
/* ((double*)*samples)[n] = wave_prop->a * (((double)wave_prop->f * n / (wave_prop->f_s * wave_prop->channels)) - (int)((double)wave_prop->f * n / (wave_prop->f_s * wave_prop->channels))); */
((double*)*samples)[n] = wave_prop->a * (2 * (((double)wave_prop->f * n / (wave_prop->f_s * wave_prop->channels)) - (int)(0.5f + ((double)wave_prop->f * n / (wave_prop->f_s * wave_prop->channels)))));
}
}
char sgn(double* x) {
char sgn = (*x >= 0) ? 1 : -1;
return sgn;
}
char convert_double_to_pcm_8bit_signed(double* sample) {
if (*sample >= 0) {
return (S8BIT_MAX * *sample);
} else {
return -(S8BIT_MIN * *sample);
}
}
short convert_double_to_pcm_16bit_signed(double* sample) {
if (*sample >= 0) {
return (S16BIT_MAX * *sample);
} else {
return -(S16BIT_MIN * *sample);
}
}
int convert_double_to_pcm_32bit_signed(double* sample) {
if (*sample >= 0) {
return (S32BIT_MAX * *sample);
} else {
return -(S32BIT_MIN * *sample);
}
}
unsigned char convert_double_to_pcm_8bit_unsigned(double* sample) {
if (*sample >= 0) {
return ((U8BIT_MAX-1)*0.5 * *sample) + (U8BIT_MAX+1)*0.5;
} else {
return ((U8BIT_MAX-1)*0.5 * *sample) + (U8BIT_MAX-1)*0.5;
}
}
unsigned short convert_double_to_pcm_16bit_unsigned(double* sample) {
if (*sample >= 0) {
return ((U16BIT_MAX-1)*0.5 * *sample) + (U16BIT_MAX+1)*0.5;
} else {
return ((U16BIT_MAX-1)*0.5 * *sample) + (U16BIT_MAX-1)*0.5;
}
}
unsigned int convert_double_to_pcm_32bit_unsigned(double* sample) {
if (*sample >= 0) {
return ((U32BIT_MAX-1)*0.5 * *sample) + (U32BIT_MAX+1)*0.5;
} else {
return ((U32BIT_MAX-1)*0.5 * *sample) + (U32BIT_MAX-1)*0.5;
}
}
void set_header_pcm(wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t* fact_chunk, data_chunk_t *data_chunk) {
/* Set as NULL for it to not be unused */
fact_chunk = (fact_chunk_t*) NULL;
/* RIFF Chunk */
strcpy(riff_chunk->chunkID, "RIFF");
strcpy(riff_chunk->waveID, "WAVE");
/* Format Chunk */
strcpy(fmt_chunk->chunkID, "fmt ");
fmt_chunk->chunk_size = 16;
fmt_chunk->wFormatTag = wave_prop->encoding;
fmt_chunk->nChannels = wave_prop->channels;
fmt_chunk->nSamplesPerSec = wave_prop->f_s;
fmt_chunk->nAvgBytesPerSec = wave_prop->f_s * wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->nBlockAlign = wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->wBitsPerSample = 8 * wave_prop->bytes_per_sample;
/* Data Chunk */
strcpy(data_chunk->chunkID, "data");
data_chunk->chunk_size = wave_prop->bytes_per_sample * wave_prop->channels * wave_prop->total_number_of_samples;
}
void encode_pcm(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
switch (wave_prop->representation) {
case 's':
switch (wave_prop->bytes_per_sample) {
case 1:
*encoded_samples = (char*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(char));
encode_pcm_signed_8bit(samples, encoded_samples, wave_prop);
break;
case 2:
*encoded_samples = (short*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(short));
encode_pcm_signed_16bit(samples, encoded_samples, wave_prop);
break;
case 3:
break;
case 4:
*encoded_samples = (int*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(int));
encode_pcm_signed_32bit(samples, encoded_samples, wave_prop);
break;
default:
printf("\nSample byte length in signed is not implemented.\n");
break;
}
break;
case 'u':
switch (wave_prop->bytes_per_sample) {
case 1:
*encoded_samples = (unsigned char*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(unsigned char));
encode_pcm_unsigned_8bit(samples, encoded_samples, wave_prop);
break;
case 2:
printf("\n\nUnsigned %d-bit PCM isn't supported by the WAVE format but sure here you go:", wave_prop->bytes_per_sample);
*encoded_samples = (unsigned short*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(unsigned short));
encode_pcm_unsigned_16bit(samples, encoded_samples, wave_prop);
break;
case 3:
printf("\n\nUnsigned %d-bit PCM isn't supported by the WAVE format but sure here you go:", wave_prop->bytes_per_sample);
break;
case 4:
printf("\n\nUnsigned %d-bit PCM isn't supported by the WAVE format but sure here you go:", wave_prop->bytes_per_sample);
*encoded_samples = (unsigned int*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(unsigned int));
encode_pcm_unsigned_32bit(samples, encoded_samples, wave_prop);
break;
default:
printf("\nSample byte length in unsigned is not implemented.\n");
break;
}
break;
default:
printf("\nRepresentation not implemented.\n");
break;
}
}
void encode_pcm_signed_8bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((char*)*encoded_samples)[n] = convert_double_to_pcm_8bit_signed(&samples[n]);
}
}
void encode_pcm_signed_16bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((short*)*encoded_samples)[n] = convert_double_to_pcm_16bit_signed(&samples[n]);
}
}
void encode_pcm_signed_32bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((int*)*encoded_samples)[n] = convert_double_to_pcm_32bit_signed(&samples[n]);
}
}
void encode_pcm_unsigned_8bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((unsigned char*)*encoded_samples)[n] = convert_double_to_pcm_8bit_unsigned(&samples[n]);
}
}
void encode_pcm_unsigned_16bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((unsigned short*)*encoded_samples)[n] = convert_double_to_pcm_16bit_unsigned(&samples[n]);
}
}
void encode_pcm_unsigned_32bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((unsigned int*)*encoded_samples)[n] = convert_double_to_pcm_32bit_unsigned(&samples[n]);
}
}
void set_header_ieee_float(wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t *fact_chunk, data_chunk_t *data_chunk) {
/* RIFF Chunk */
strcpy(riff_chunk->chunkID, "RIFF");
strcpy(riff_chunk->waveID, "WAVE");
/* Format Chunk */
strcpy(fmt_chunk->chunkID, "fmt ");
fmt_chunk->chunk_size = 18;
fmt_chunk->wFormatTag = wave_prop->encoding;
fmt_chunk->nChannels = wave_prop->channels;
fmt_chunk->nSamplesPerSec = wave_prop->f_s;
fmt_chunk->nAvgBytesPerSec = wave_prop->f_s * wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->nBlockAlign = wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->wBitsPerSample = 8 * wave_prop->bytes_per_sample;
fmt_chunk->cbSize = 0;
/* Fact Chunk */
strcpy(fact_chunk->chunkID, "fact");
fact_chunk->chunk_size = 4;
fact_chunk->dwSampleLength = wave_prop->channels * wave_prop->total_number_of_samples;
/* Data Chunk */
strcpy(data_chunk->chunkID, "data");
data_chunk->chunk_size = wave_prop->bytes_per_sample * wave_prop->channels * wave_prop->total_number_of_samples;
/* Check if padding is necessary */
if (data_chunk->chunk_size % 2 != 0) {
wave_prop->padding = 1;
}
}
void encode_ieee_float(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
switch (wave_prop->bytes_per_sample) {
case 4:
*encoded_samples = (float*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(float));
encode_ieee_float_32bit(samples, encoded_samples, wave_prop);
break;
case 8:
*encoded_samples = (double*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(double));
encode_ieee_float_64bit(samples, encoded_samples, wave_prop);
break;
default:
printf("\nSample byte length is not implemented.\n");
break;
}
}
void encode_ieee_float_32bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((float*)*encoded_samples)[n] = (float)samples[n];
}
}
void encode_ieee_float_64bit(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
((double*)*encoded_samples)[n] = samples[n];
}
}
void set_header_a_law(wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t* fact_chunk, data_chunk_t *data_chunk) {
/* Default to 1 byte of data */
wave_prop->bytes_per_sample = 1;
/* RIFF Chunk */
strcpy(riff_chunk->chunkID, "RIFF");
strcpy(riff_chunk->waveID, "WAVE");
/* Format Chunk */
strcpy(fmt_chunk->chunkID, "fmt ");
fmt_chunk->chunk_size = 18;
fmt_chunk->wFormatTag = wave_prop->encoding;
fmt_chunk->nChannels = wave_prop->channels;
fmt_chunk->nSamplesPerSec = wave_prop->f_s;
fmt_chunk->nAvgBytesPerSec = wave_prop->f_s * wave_prop->channels * wave_prop->bytes_per_sample; // 8 bit data
fmt_chunk->nBlockAlign = wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->wBitsPerSample = 8 * wave_prop->bytes_per_sample;
fmt_chunk->cbSize = 0;
/* Fact Chunk */
strcpy(fact_chunk->chunkID, "fact");
fact_chunk->chunk_size = 4;
fact_chunk->dwSampleLength = wave_prop->channels * wave_prop->total_number_of_samples;
/* Data Chunk */
strcpy(data_chunk->chunkID, "data");
data_chunk->chunk_size = wave_prop->bytes_per_sample * wave_prop->channels * wave_prop->total_number_of_samples;
/* Check if padding is necessary */
if (data_chunk->chunk_size % 2 != 0) {
wave_prop->padding = 1;
}
}
/* Based on the Wikipedia equation. Does not work! Left in to show attempt. Issue with x = 0. */
char a_law_compress_old(double* x) {
double resd = 0.0f;
char resc = 0;
char sgn = (*x >= 0) ? 1 : -1;
if (fabs(*x) < (1/A)) {
resd = sgn * ((A * fabs(*x)) / (1 + log(A)));
} else if ((fabs(*x) >= (1/A)) && (fabs(*x) <= 1)) {
resd = sgn * ((1 + log(A * fabs(*x))) / (1 + log(A)));
}
if (resd >= 0) {
resc = (S8BIT_MAX * resd);
} else {
resc = -(S8BIT_MIN * resd);
}
/* resc = resc ^ (0x55); #<{(| toggle even bits? not in wikipedia equation |)}># */
return resc;
}
/* Read license in the ITU-T code and attribute accordingly. Mention that the code was changed (also change it more) and is based on that. */
char a_law_compress(short* x) {
short ix, exp;
short out;
ix = *x < 0 /* 0 <= ix < 2048 */
? (~(*x)) >> 4 /* 1's complement for negative values */
: (*x) >> 4;
/* Do more, if exponent > 0 */
if (ix > 15) { /* exponent=0 for ix <= 15 */
exp = 1; /* first step: */
while (ix > 16 + 15) { /* find mantissa and exponent */
ix >>= 1;
exp++;
}
ix -= 16; /* second step: remove leading '1' */
ix += exp << 4; /* now compute encoded value */
}
if (*x >= 0)
ix |= (0x0080); /* add sign bit */
out = ix ^ (0x0055); /* toggle even bits */
return out;
}
void encode_a_law(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
short pcm_sample;
*encoded_samples = (char*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(char));
for(int n = 0; n < wave_prop->total_number_of_samples; n++) {
pcm_sample = convert_double_to_pcm_16bit_signed(&samples[n]);
((char*)*encoded_samples)[n] = a_law_compress(&pcm_sample);
}
}
void set_header_mu_law(wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t* fact_chunk, data_chunk_t *data_chunk) {
/* Default to 1 byte of data */
wave_prop->bytes_per_sample = 1;
/* RIFF Chunk */
strcpy(riff_chunk->chunkID, "RIFF");
strcpy(riff_chunk->waveID, "WAVE");
/* Format Chunk */
strcpy(fmt_chunk->chunkID, "fmt ");
fmt_chunk->chunk_size = 18;
fmt_chunk->wFormatTag = wave_prop->encoding;
fmt_chunk->nChannels = wave_prop->channels;
fmt_chunk->nSamplesPerSec = wave_prop->f_s;
fmt_chunk->nAvgBytesPerSec = wave_prop->f_s * wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->nBlockAlign = wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->wBitsPerSample = 8 * wave_prop->bytes_per_sample;
fmt_chunk->cbSize = 0;
/* Fact Chunk */
strcpy(fact_chunk->chunkID, "fact");
fact_chunk->chunk_size = 4;
fact_chunk->dwSampleLength = wave_prop->channels * wave_prop->total_number_of_samples;
/* Data Chunk */
strcpy(data_chunk->chunkID, "data");
data_chunk->chunk_size = wave_prop->bytes_per_sample * wave_prop->channels * wave_prop->total_number_of_samples;
/* Check if padding is necessary */
if (data_chunk->chunk_size % 2 != 0) {
wave_prop->padding = 1;
}
}
/* Based on the Wikipedia equation. Does not work! Sometimes values agree. */
char mu_law_compress_old(double* x) {
double resd = 0.0f;
char resc = 0;
char sgn = (*x >= 0) ? 1 : -1;
resd = sgn * (log(1 + MU * fabs(*x)))/(log(1 + MU));
if (resd >= 0) {
resc = (S8BIT_MAX * resd);
} else {
resc = -(S8BIT_MIN * resd);
}
return resc;
}
/* Read license in the ITU-T code and attribute accordingly. Mention that the code was changed (also change it more) and is based on that. */
char mu_law_compress(short *x) {
short segment; /* segment (Table 2/G711, column 1) */
short out;
short low_nibble, high_nibble; /* low/high nibble of log companded sample */
/* Change from 14 bit left justified to 14 bit right justified.
* Compute absolute value; adjust for easy processing */
/* Compute 1's complement */
short absval = *x < 0 ? ((~(*x)) >> 2) + 33 : ((*x) >> 2) + 33; /* Absolute value */
/* 33 is the difference value between the thresholds for A-law and u-law. */
if (absval > (0x1FFF)) /* limit to < 8192 */
absval = (0x1FFF);
/* Determination of sample's segment */
short temp = absval >> 6;
segment = 1;
while (temp != 0) {
segment++;
temp >>= 1;
}
/* Mounting the high-nibble of the log-PCM sample */
high_nibble = (0x0008) - segment;
/* Mounting the low-nibble of the log PCM sample. Right shift of mantissa and masking away the leading '1' */
low_nibble = (absval >> segment) & (0x000F);
low_nibble = (0x000F) - low_nibble;
/* Joining the high-nibble and the low-nibble of the log PCM sample */
out = (high_nibble << 4) | low_nibble;
/* Add sign bit */
if (*x >= 0)
out = out | (0x0080);
return out;
}
void encode_mu_law(double* samples, void** encoded_samples, wave_prop_t* wave_prop) {
short pcm_sample;
*encoded_samples = (char*) malloc(wave_prop->total_number_of_samples * wave_prop->channels * sizeof(char));
for(int n = 0; n < wave_prop->total_number_of_samples * wave_prop->channels; n++) {
pcm_sample = convert_double_to_pcm_16bit_signed(&samples[n]);
((char*)*encoded_samples)[n] = mu_law_compress(&pcm_sample);
}
}
void set_header_extensible(wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t* fact_chunk, data_chunk_t *data_chunk) {
/* RIFF Chunk */
strcpy(riff_chunk->chunkID, "RIFF");
strcpy(riff_chunk->waveID, "WAVE");
/* Format Chunk */
strcpy(fmt_chunk->chunkID, "fmt ");
fmt_chunk->chunk_size = 40;
fmt_chunk->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
fmt_chunk->nChannels = wave_prop->channels;
fmt_chunk->nSamplesPerSec = wave_prop->f_s;
fmt_chunk->nAvgBytesPerSec = wave_prop->f_s * wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->nBlockAlign = wave_prop->channels * wave_prop->bytes_per_sample;
fmt_chunk->wBitsPerSample = 8 * wave_prop->bytes_per_sample;
fmt_chunk->cbSize = 22;
fmt_chunk->wValidBitsPerSample = wave_prop->valid_bits;
fmt_chunk->dwChannelMask = wave_prop->channel_mask; // from 0 to 4 294 967 295
memcpy(fmt_chunk->SubFormat, "xx\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71", 16); // xx gets replaced with the next line.
memcpy(fmt_chunk->SubFormat, &wave_prop->encoding, 2);
/* Fact Chunk */
strcpy(fact_chunk->chunkID, "fact");
fact_chunk->chunk_size = 4;
fact_chunk->dwSampleLength = wave_prop->channels * wave_prop->total_number_of_samples;
/* Data Chunk */
strcpy(data_chunk->chunkID, "data");
data_chunk->chunk_size = wave_prop->bytes_per_sample * wave_prop->channels * wave_prop->total_number_of_samples;
/* Check if padding is necessary */
if (data_chunk->chunk_size % 2 != 0) {
wave_prop->padding = 1;
}
}
void output_pcm(FILE * file, void* sampled_data, wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t *fact_chunk, data_chunk_t *data_chunk) {
/* Calculating the unused format chunk members to fix the effect of padding */
size_t unused_fmt_chunk = sizeof(fmt_chunk->cbSize) + sizeof(fmt_chunk->wValidBitsPerSample) + sizeof(fmt_chunk->dwChannelMask) + sizeof(fmt_chunk->SubFormat);
size_t used_fmt_chunk = sizeof(*fmt_chunk) - unused_fmt_chunk;
riff_chunk->chunk_size = sizeof(riff_chunk->waveID) + used_fmt_chunk + sizeof(*data_chunk) + (wave_prop->total_number_of_samples * wave_prop->bytes_per_sample * wave_prop->channels) + wave_prop->padding;
fwrite(riff_chunk, sizeof(riff_chunk_t), 1, file);
fwrite(fmt_chunk, used_fmt_chunk, 1, file);
fwrite(data_chunk, sizeof(data_chunk_t), 1, file);
fwrite(sampled_data, wave_prop->total_number_of_samples * wave_prop->bytes_per_sample * wave_prop->channels, 1, file);
/* Padding added based on if the data chunk size is odd or even */
if (wave_prop->padding) {
uint8_t padding = 0;
fwrite(&padding, sizeof(padding), 1, file);
}
}
void output_non_pcm(FILE * file, void* sampled_data, wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t *fact_chunk, data_chunk_t *data_chunk) {
/* Calculating the unused format chunk members to fix the effect of padding */
size_t unused_fmt_chunk = sizeof(fmt_chunk->wValidBitsPerSample) + sizeof(fmt_chunk->dwChannelMask) + sizeof(fmt_chunk->SubFormat);
size_t used_fmt_chunk = sizeof(*fmt_chunk) - unused_fmt_chunk;
riff_chunk->chunk_size = sizeof(riff_chunk->waveID) + used_fmt_chunk + sizeof(*fact_chunk) + sizeof(*data_chunk) + (wave_prop->total_number_of_samples * wave_prop->bytes_per_sample * wave_prop->channels) + wave_prop->padding;
fwrite(riff_chunk, sizeof(riff_chunk_t), 1, file);
fwrite(fmt_chunk, used_fmt_chunk, 1, file);
fwrite(fact_chunk, sizeof(fact_chunk_t), 1, file);
fwrite(data_chunk, sizeof(data_chunk_t), 1, file);
fwrite(sampled_data, wave_prop->total_number_of_samples * wave_prop->bytes_per_sample * wave_prop->channels, 1, file);
/* Padding added based on if the data chunk size is odd or even */
if (wave_prop->padding) {
uint8_t padding = 0;
fwrite(&padding, sizeof(padding), 1, file);
}
}
void output_extensible(FILE * file, void* sampled_data, wave_prop_t* wave_prop, riff_chunk_t *riff_chunk, fmt_chunk_t *fmt_chunk, fact_chunk_t *fact_chunk, data_chunk_t *data_chunk) {
riff_chunk->chunk_size = sizeof(riff_chunk->waveID) + sizeof(*fmt_chunk) + sizeof(*fact_chunk) + sizeof(*data_chunk) + (wave_prop->total_number_of_samples * wave_prop->bytes_per_sample * wave_prop->channels) + wave_prop->padding;
fwrite(riff_chunk, sizeof(riff_chunk_t), 1, file);
fwrite(fmt_chunk, sizeof(fmt_chunk_t), 1, file);
/* o_byte cue_chunk[60]; */
/* memcpy(cue_chunk, "\x00", 60); */
/* memcpy(cue_chunk, "cue \x34", 5); */
/* fwrite(cue_chunk, sizeof(cue_chunk), 1, file); */
fwrite(fact_chunk, sizeof(fact_chunk_t), 1, file);
fwrite(data_chunk, sizeof(data_chunk_t), 1, file);
fwrite(sampled_data, wave_prop->total_number_of_samples * wave_prop->bytes_per_sample * wave_prop->channels, 1, file);
/* printf("%ld\n", fmt_chunk->dwChannelMask); */
/* printf("%d\n", fmt_chunk->wValidBitsPerSample); */
/* Padding added based on if the data chunk size is odd or even */
if (wave_prop->padding) {
uint8_t padding = 0;
fwrite(&padding, sizeof(padding), 1, file);
}
}
void output_file_details(wave_prop_t* wave_prop) {
printf("\n\tFile Name:\t%s"
"\n\tWave Type:\t%s"
"\n\tSize:\t\t%lld"
"\n\tDuration:\t%f"
"\n\tEncoding:\t%s"
"\n\tSampling Freq.:\t%u"
"\n\tTone Freq.:\t%u"
"\n\tWave Period:\t%f"
"\n\tTotal Samples:\t%lld"
"\n\tChannels:\t%d"
"\n\tSample Length:\t%d"
"\n\n"
, wave_prop->file_name, wave_prop->typestr, wave_prop->size, wave_prop->duration, wave_prop->encodingstr, wave_prop->f_s, wave_prop->f, wave_prop->p, wave_prop->total_number_of_samples, wave_prop->channels, wave_prop->bytes_per_sample * 8);
}