aboutsummaryrefslogtreecommitdiff
path: root/src/raw_wav_hrtfcont.cpp
blob: 52bf82b70dfaf12bbe40d0f2e31bd91de3758d3c (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
/*
    cw_binaural~: a binaural synthesis external for pure data
    by David Doukhan - david.doukhan@gmail.com - http://www.limsi.fr/Individu/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 <regex.h>
#include <stdlib.h>
#include <dirent.h>
#include <sndfile.h>
#include <assert.h>
#include "raw_wav_hrtfcont.hpp"
#include "logstring.hpp"



void RawWavHrtfCont::set_ir_buffer_from_path(ir_buffer& irb, string path)
{
  SF_INFO	sfinfo;
  SNDFILE*	sndfd;

  // slog << "Trying " << path << std::endl;
  //irb.fname = path.c_str();

  // opening the sound file
  sfinfo.format = 0;
  sndfd = sf_open(path.c_str(), SFM_READ, &sfinfo);
  if (!sndfd)
    {
      slog << "failed to open " << path << endl;
      throw 0;
    }

  //slog << sfinfo.channels << endl;
  if (sfinfo.channels != 2)
    {
      slog << path << " has " << sfinfo.channels << " channels instead of 2!" << endl;
      throw 0;
    }
  assert(sfinfo.samplerate == 44100);

  //slog << "frames" << sfinfo.frames << endl;
  const size_t wav_length = sfinfo.frames;// / 2;
  //slog << "ir length before" << _ir_length << endl;
  if (!_ir_length)
    // we're extrating the first impulse response
    // all other impulse response extrated
    // must have the same length
    _ir_length = wav_length;
  //slog << "ir length" << _ir_length << endl;

  // if the considered file is empty
  // or if its size is different from previously
  // extracted impulse response files
  assert(wav_length && (wav_length == _ir_length));

  float* wavdata = new float[sfinfo.frames*2];
  sf_read_float(sndfd, wavdata, sfinfo.frames*2);

  irb.lbuf = new float[wav_length];
  irb.rbuf = new float[wav_length];


  for (size_t i = 0; i < wav_length; ++i)
    {
      irb.lbuf[i] = wavdata[i*2];
      irb.rbuf[i] = wavdata[i*2+1];
    }

  delete [] wavdata;
  sf_close(sndfd);
}

// constructor should parse the directory
// deduce the how many different azimuths
// and elevations are present, according
// to a given regexp
// => intermediate list structure
// then use a faster structure to store
// the extracted wavs!!!
// FIXME: check the map size is != 0
RawWavHrtfCont::RawWavHrtfCont(const ir_key& k):
  HrtfCont(k)
{
  // regex vars
  int		re_status;
  regex_t	re;
  regmatch_t	pmatch[3];
  // directory parsing vars
  DIR		*dp;
  struct dirent *dirp;
  // other
  float		az, elev;
  
  _ir_length = 0;

  // slog << "Raw Wav HRTF CONT " << k.iir_regex << endl;
  // compile the regex
  if (regcomp(&re, k.iir_regex.c_str(), REG_EXTENDED|REG_ICASE))
    {
      slog << "regex: " << k.iir_regex << " is not supported!" << endl;
      throw 0;
    }

  // test existence of directory supposed to store hrtf impulse responses
  if((dp  = opendir(k.path.c_str())) == NULL)
    {
      slog << "directory " << k.path << " does not exist!" << endl;
      throw 0;
    }

  // extract wav files matching the regex
  // contained in path
  while ((dirp = readdir(dp)))
    //{ slog << dirp->d_name << endl;
    if (!regexec(&re, dirp->d_name, 3, pmatch, REG_NOTBOL|REG_NOTEOL))
      {
	//slog << "match!" << endl;
	string dirname(dirp->d_name);
	string group0 = dirname.substr(pmatch[1].rm_so, pmatch[1].rm_eo);
	string group1 = dirname.substr(pmatch[2].rm_so, pmatch[2].rm_eo);
	
	if (k.is_azimuth_first)
	  {
	    az = atof(group0.c_str());
	    elev = atof(group1.c_str());
	  }
	else
	  {
	    az = atof(group1.c_str());
	    elev = atof(group0.c_str());
	  }
	// slog << "(az,elev)=(" << az << ", " << elev << ") for file " << dirname << endl;
	
	if (k.vertical_polar_coords)
	  {
	    normalize_vertpolar_coords(az, elev);
	    set_ir_buffer_from_path(_m[elev][az], k.path + "/" + dirname);
	  }
	else
	  {
	    // TO BE IMPROVED
	    if (elev > 180)
	      elev -= 360;
	    set_ir_buffer_from_path(_m[az][elev], k.path + "/" + dirname);
	  }

      }
  
  // close the directory structure
  closedir(dp);

  // free the regex
  regfree(&re);

  //slog << "END OF RAW WAV HRTF CONT" << endl;
}