aboutsummaryrefslogtreecommitdiff
path: root/src/cw_binaural~.cpp
blob: f315fa010c3682dd525bc3d00b5350bc18f96c7e (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
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
/*
    cw_binaural~: a binaural synthesis external for pure data
    by David Doukhan - david.doukhan@gmail.com - http://perso.limsi.fr/doukhan
    and Anne Sedes - sedes.anne@gmail.com
    Copyright (C) 2009-2011  David Doukhan and Anne Sedes

    For more details, see CW_binaural~, a binaural synthesis external for Pure Data
    David Doukhan and Anne Sedes, PDCON09


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <m_pd.h>
#include "binaural_processor.hpp"
#include "logstring.hpp"

static t_class *cw_binaural_tilde_class;

typedef struct _cw_binaural_tilde
{
	t_object		x_obj;
	BinauralProcessor	*bp;
	float			default_input;
}	t_cw_binaural_tilde;


t_int *cw_binaural_tilde_perform(t_int *w)
{
  t_cw_binaural_tilde *x = (t_cw_binaural_tilde *)(w[1]);

  // 3 signal input: signal, azimuth and elevation
  t_sample  *input = (t_sample *)(w[2]);
  t_sample  *azimuths = (t_sample *)(w[3]);
  t_sample  *elevations = (t_sample *)(w[4]);

  // 2 signal outputs: for the left and right ear
  t_sample *left_output = (t_sample *)(w[5]);
  t_sample *right_output = (t_sample *)(w[6]);

  int n = (int)(w[7]);
  
  x->bp->process(input, azimuths, elevations, left_output, right_output, n);

  return (w+8);
}





void cw_binaural_tilde_dsp(t_cw_binaural_tilde *x, t_signal **sp)
{
  dsp_add(cw_binaural_tilde_perform, 7, x, sp[0]->s_vec, sp[1]->s_vec,
	  sp[2]->s_vec, sp[3]->s_vec, sp[4]->s_vec,
	  sp[0]->s_n);
}

// called when the message listen_db is sent to the object
void cw_binaural_set_listen_db(t_cw_binaural_tilde *x, t_symbol *hrtf_path)
{
  try
    {
      x->bp->set_listen_db(string(hrtf_path->s_name));
    }
  catch (int n) {}
  logstring2pdterm();
}

// called when the message cipic_db is sent to the object
void cw_binaural_set_cipic_db(t_cw_binaural_tilde *x, t_symbol *hrtf_path)
{
  try
    {
      x->bp->set_cipic_db(string(hrtf_path->s_name));
    }
  catch (int n) {}
  logstring2pdterm();
}

// convert a symbol to boolean
static bool	symb2bool(t_symbol* symb)
{
  string	s(symb->s_name);

  for (size_t i = 0; i < s.length(); ++i)
    s[i] = toupper(s[i]);
  return s == "TRUE";
}

// called when the message sethrtf is sent to the object
// message allowing to load any hrtf database
// hrtf_path correspond to the path to the directory storing the hrtf as wav files
// fname_regex is a regex representing the name of files containing impulse responses
// it should contain a group for each parameter to extract (azimuth and elevation)
// az_first tells wether the first group corresponds to azimuth or not
// vertical_polar_coords is set to true if the database is expressed in vertical polar
// coordinates, if set to false, it refers interaural polar coordinates
void cw_binaural_set_hrtf_db(t_cw_binaural_tilde *x,
			     t_symbol *symb_hrtf_path,
			     t_symbol *symb_fname_regex,
			     t_symbol *symb_az_first,
			     t_symbol *symb_vertical_polar_coords)
{
  string	hrtf_path(symb_hrtf_path->s_name);
  string	fname_regex(symb_fname_regex->s_name);
  bool		az_first = symb2bool(symb_az_first);
  bool		vertical_polar_coords = symb2bool(symb_vertical_polar_coords);

  // Pd does not allows to transmit backslashes in messages,
  // so we replace each occurence of double slashes with a back slash
  size_t	found;
  while ((found = fname_regex.find("//")) != string::npos)
    fname_regex.replace(found, 2, 1, '\\');

  try 
    {
      x->bp->set_hrtf(hrtf_path, fname_regex, az_first, vertical_polar_coords);
    }
  catch (int n) {}
  logstring2pdterm();
}



void cw_binaural_tilde_free(t_cw_binaural_tilde *x)
{
  if (x->bp)
    delete x->bp;
  logstring2pdterm();
}

void *cw_binaural_tilde_new(t_symbol *obj_name, int argc, t_atom *argv)

{
  // default creation arguments
  int impulse_response_size = 128;
  std::string filtering_method = std::string("RIFF");
  std::string delay_method = std::string("Hermite4");
  
  // parsing creation arguments
  switch (argc < 3 ? argc : 3)
    {
    case 3:
      delay_method = std::string(atom_getsymbol(argv + 2)->s_name);
    case 2:
      filtering_method = std::string(atom_getsymbol(argv + 1)->s_name);
    case 1:
      impulse_response_size = (int) atom_getfloat(argv);
    default:
      break;
    }

  // creating object
  t_cw_binaural_tilde *x = (t_cw_binaural_tilde *)pd_new(cw_binaural_tilde_class);
  x->default_input = 0;
  x->bp = NULL;

  try
    {
      // call to the constructor
      x->bp = new BinauralProcessor(impulse_response_size, filtering_method, delay_method);
    }
  catch (int n)
    {
      x->bp = 0;
      logstring2pdterm();
      return (void*) 0;
    }

  // azimuth inlet
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
  // elevation inlet
  inlet_new(&x->x_obj , &x->x_obj.ob_pd, &s_signal, &s_signal);

  // left outlet
  outlet_new(&x->x_obj, &s_signal);
  // right outlet
  outlet_new(&x->x_obj, &s_signal);

  logstring2pdterm();

  return (void *)x;
}


extern "C"
#ifdef WIN32
__declspec(dllexport) void cw_binaural_tilde_setup(void)
#else
  void cw_binaural_tilde_setup(void)
#endif
{
  // creation of the cw_binaural~ instance
  cw_binaural_tilde_class =
    class_new(gensym("cw_binaural~"),
	      (t_newmethod)cw_binaural_tilde_new,
	      (t_method) cw_binaural_tilde_free, sizeof(t_cw_binaural_tilde),
	      CLASS_DEFAULT, 
	      A_GIMME, 0);
  
  // sound processing
  class_addmethod(cw_binaural_tilde_class,
		  (t_method)cw_binaural_tilde_dsp, gensym("dsp"), (t_atomtype) 0);

  // management of messages used to set a listen database
  class_addmethod(cw_binaural_tilde_class,
		  (t_method)cw_binaural_set_listen_db,
		  gensym("listen_db"),
		  A_DEFSYMBOL, (t_atomtype) 0);

  // management of messages used to set a CIPIC database
  class_addmethod(cw_binaural_tilde_class,
		  (t_method)cw_binaural_set_cipic_db,
		  gensym("cipic_db"),
		  A_DEFSYMBOL, (t_atomtype) 0);

  // management of messages used to set any database
  class_addmethod(cw_binaural_tilde_class,
		  (t_method)cw_binaural_set_hrtf_db,
		  gensym("set_hrtf_db"),
		  A_DEFSYMBOL, A_DEFSYMBOL, A_DEFSYMBOL, A_DEFSYMBOL, (t_atomtype) 0);
		
  CLASS_MAINSIGNALIN(cw_binaural_tilde_class, t_cw_binaural_tilde, default_input);
}