summaryrefslogtreecommitdiff
path: root/utils/conf_bridge_binaural_hrir_importer.c
blob: c1e5458db013940a07eb54415391d5d6612ba90e (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2016, Frank Haase, Dennis Guse
 *
 * Frank Haase <fra.haase@gmail.com>
 * Dennis Guse <dennis.guse@alumni.tu-berlin.de>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * Converts a Head Related Impulse Response (HRIR) database (a multi-channel wave) into a C header file.
 * HRIR for the left ear and HRIR for right ear have to be interleaved.
 * No further signal processing is applied (e.g., resampling).
 *
 * Info messages are printed to stderror and the generated header file to output.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sndfile.h>
#include "conf_bridge_binaural_hrir_importer.h"

int main (int argc, char **argv)
{
	char *hrir_filename;
	unsigned int binaural_index_start;
	unsigned int binaural_index_end;

	SNDFILE *hrir_file;
	SF_INFO hrir_info;
	float *hrir_data;

	unsigned int impulse_response_index_start;
	unsigned int impulse_response_index_end;

	int j;
	int ir_current;

	if(argc != 4) {
		puts("HRIR database to C header file converter.");
		puts("Usage: conf_bridge_binaural_hrir_importer HRIR.wav INDEX_START INDEX_END > OUTPUT.h");
		puts("Example: conf_bridge_binaural_hrir_importer hrirs.wav 0 180 > ../bridges/bridge_softmix/include/hrirs.h");

		return -1;
	}

	/* Parse arguments */
	hrir_filename = argv[1];
	binaural_index_start = atoi(argv[2]);
	binaural_index_end = atoi(argv[3]);

	/* Read HRIR database */
	hrir_file = sf_open(hrir_filename, SFM_READ, &hrir_info);
	if(hrir_file == NULL) {
		fprintf(stderr, "ERROR: Could not open HRIR database (%s).\n", hrir_filename);

		return -1;
	}
	fprintf(stderr, "INFO: Opened HRIR database (%s) with: number channels: %d; samplerate: %d; samples per channel: %ld\n", hrir_filename, hrir_info.channels, hrir_info.samplerate, hrir_info.frames);

	hrir_data = (float *)malloc(hrir_info.channels * hrir_info.frames * sizeof(float));
	if(hrir_data == NULL) {
		fprintf(stderr, "ERROR: Out of memory!");

		return -1;
	}

	/* Channels are interleaved */
	sf_read_float(hrir_file, hrir_data, hrir_info.channels * hrir_info.frames);
	sf_close(hrir_file);

	if(binaural_index_start >= binaural_index_end) {
		fprintf(stderr, "ERROR: INDEX_START (%d) must be smaller than INDEX_END (%d).", binaural_index_start, binaural_index_end);
		free(hrir_data);

		return -1;
	}

	if (binaural_index_end * 2 >= hrir_info.channels) {
		fprintf(stderr, "ERROR: END_INDEX (%d) is out of range for HRIR database (%s).\n", binaural_index_end, hrir_filename);
		free(hrir_data);

		return -1;
	}

	/* Convert indices */
	impulse_response_index_start = 2 * binaural_index_start;
	impulse_response_index_end = (binaural_index_end + 1) * 2;

	/* Write header */
	printf(FILE_HEADER, hrir_filename, binaural_index_start, binaural_index_end);

	printf("#define HRIRS_IMPULSE_LEN %ld\n", hrir_info.frames);
	printf("#define HRIRS_IMPULSE_SIZE %d\n", binaural_index_end - binaural_index_start + 1);
	printf("#define HRIRS_SAMPLE_RATE %d\n\n", hrir_info.samplerate);

	printf("float hrirs_left[HRIRS_IMPULSE_SIZE][HRIRS_IMPULSE_LEN] = {\n");
	for (ir_current = impulse_response_index_start; ir_current < impulse_response_index_end; ir_current += 2) {
		printf("{");

		for (j = 0; j < hrir_info.frames - 1; j++) {
			printf("%.16f,%s", hrir_data[ir_current * hrir_info.frames + j], ((j + 1) % 4 ? " " : "\n"));
		}
		/* Write last without trailing "," */
		printf("%.16f", hrir_data[ir_current * hrir_info.frames + hrir_info.frames - 1]);

		if (ir_current + 2 < impulse_response_index_end) {
			printf("},\n");
		}	else {
			printf("}};");
		}
	}

	printf("\nfloat hrirs_right[HRIRS_IMPULSE_SIZE][HRIRS_IMPULSE_LEN] = {\n");
	for (ir_current = impulse_response_index_start + 1; ir_current < impulse_response_index_end + 1; ir_current += 2) {
		printf("{");

		for (j = 0; j < hrir_info.frames - 1; j++) {
			printf("%.16f,%s", hrir_data[ir_current * hrir_info.frames + j], ((j + 1) % 4 ? " " : "\n"));
		}
		 /* Write last without trailing "," */
		printf("%.16f", hrir_data[ir_current * hrir_info.frames + hrir_info.frames - 1]);

		if (ir_current + 2 < impulse_response_index_end) {
			printf("},\n");
		}	else {
			printf("}};");
		}
	}

	fprintf(stderr, "INFO: Successfully converted: imported %d impulse responses.\n", impulse_response_index_end - impulse_response_index_start);
	free(hrir_data);

	return 0;
}