|
| 1 | +#include <sys/stat.h> |
| 2 | +#include <sys/types.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <mysofa.h> |
| 6 | + |
| 7 | +int main(int argc, char **argv) |
| 8 | +{ |
| 9 | + struct MYSOFA_HRTF *hrtf; |
| 10 | + int sample_rate; |
| 11 | + int err, i, j; |
| 12 | + |
| 13 | + if (argc < 3) { |
| 14 | + printf("usage: %s input_SOFA_file output_directory\n", argv[0]); |
| 15 | + return 1; |
| 16 | + } |
| 17 | + |
| 18 | + hrtf = mysofa_load(argv[1], &err); |
| 19 | + if (!hrtf || err) { |
| 20 | + printf("invalid input SOFA file: %s\n", argv[1]); |
| 21 | + return 1; |
| 22 | + } |
| 23 | + |
| 24 | + if (hrtf->DataSamplingRate.elements != 1) |
| 25 | + goto fail; |
| 26 | + sample_rate = hrtf->DataSamplingRate.values[0]; |
| 27 | + |
| 28 | + err = mkdir(argv[2], 0744); |
| 29 | + if (err) |
| 30 | + goto fail; |
| 31 | + |
| 32 | + err = chdir(argv[2]); |
| 33 | + if (err) |
| 34 | + goto fail; |
| 35 | + |
| 36 | + for (i = 0; i < hrtf->M; i++) { |
| 37 | + FILE *file; |
| 38 | + int bps = 32; |
| 39 | + int blkalign = 8; |
| 40 | + int bytespersec = blkalign * sample_rate; |
| 41 | + char filename[1024]; |
| 42 | + int azi = hrtf->SourcePosition.values[i * 3]; |
| 43 | + int ele = hrtf->SourcePosition.values[i * 3 + 1]; |
| 44 | + int dis = hrtf->SourcePosition.values[i * 3 + 2]; |
| 45 | + int size = 8 * hrtf->N; |
| 46 | + int offset = i * 2 * hrtf->N; |
| 47 | + |
| 48 | + snprintf(filename, sizeof(filename), "azi_%d_ele_%d_dis_%d.wav", azi, ele, dis); |
| 49 | + file = fopen(filename, "w+"); |
| 50 | + fwrite("RIFF", 4, 1, file); |
| 51 | + fwrite("\xFF\xFF\xFF\xFF", 4, 1, file); |
| 52 | + fwrite("WAVE", 4, 1, file); |
| 53 | + fwrite("fmt ", 4, 1, file); |
| 54 | + fwrite("\x10\x00\00\00", 4, 1, file); |
| 55 | + fwrite("\x03\x00", 2, 1, file); |
| 56 | + fwrite("\x02\x00", 2, 1, file); |
| 57 | + fwrite(&sample_rate, 4, 1, file); |
| 58 | + fwrite(&bytespersec, 4, 1, file); |
| 59 | + fwrite(&blkalign, 2, 1, file); |
| 60 | + fwrite(&bps, 2, 1, file); |
| 61 | + fwrite("data", 4, 1, file); |
| 62 | + fwrite(&size, 4, 1, file); |
| 63 | + |
| 64 | + for (j = 0; j < hrtf->N; j++) { |
| 65 | + float l, r; |
| 66 | + |
| 67 | + l = hrtf->DataIR.values[offset + j]; |
| 68 | + r = hrtf->DataIR.values[offset + j + hrtf->N]; |
| 69 | + fwrite(&l, 4, 1, file); |
| 70 | + fwrite(&r, 4, 1, file); |
| 71 | + } |
| 72 | + fclose(file); |
| 73 | + } |
| 74 | + |
| 75 | +fail: |
| 76 | + mysofa_free(hrtf); |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
0 commit comments