ReMAS  1.5
Real-time Musical Accompaniment System
SoundFunctions.h
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2017 by "Information Retrieval and Parallel Computing" *
3  * group (University of Oviedo, Spain), "Interdisciplinary Computation *
4  * and Communication" group (Polytechnic University of Valencia, Spain) *
5  * and "Signal Processing and Telecommunication Systems Research" group *
6  * (University of Jaen, Spain) *
7  * Contact: remaspack@gmail.com *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  **************************************************************************
24 */
36 #pragma once
37 
38 #ifndef ASOUND_H
39 #define ASOUND_H
40 
41 #include "defines.h"
42 #include <strings.h>
43 
44 #ifdef ALSA
45  #include <asoundlib.h>
46  snd_pcm_uframes_t SetMicParams(snd_pcm_t **, DTWconst);
47 
56  snd_pcm_uframes_t SetMicParams(snd_pcm_t **DeviceID, DTWconst Param)
57  {
58  snd_pcm_hw_params_t *HwParams;
59  snd_pcm_uframes_t BufferSize=0;
60  snd_pcm_format_t format;
61  snd_pcm_access_t acces;
62  unsigned int tmp, rrate;
63 
64  /* Blocking aperture, last parameter=0 */
65  CHECKERR(snd_pcm_open(DeviceID, Param.SoundID, SND_PCM_STREAM_CAPTURE, 0));
66 
67  /* Memory for hardware parameters structure */
68  CHECKERR(snd_pcm_hw_params_malloc(&HwParams));
69 
70  /* Fill hardware structure with all permitted values */
71  CHECKERR(snd_pcm_hw_params_any((*DeviceID), HwParams));
72 
73  /* Set access mode */
74  CHECKERR(snd_pcm_hw_params_set_access((*DeviceID), HwParams, AlsaAccessMode));
75 
76  /* Set format */
77  CHECKERR(snd_pcm_hw_params_set_format((*DeviceID), HwParams, AlsaAccessFormat));
78 
79  /* Set number input channels */
80  CHECKERR(snd_pcm_hw_params_set_channels((*DeviceID), HwParams, AlsaChannels));
81 
82  /* Set rate */
83  rrate=AlsaRate;
84  CHECKERR(snd_pcm_hw_params_set_rate_near((*DeviceID), HwParams, &rrate, 0));
85 
86  /* Apply the hardware parameters that we've set. */
87  CHECKERR(snd_pcm_hw_params((*DeviceID), HwParams));
88 
89  /* Get the buffer size */
90  CHECKERR(snd_pcm_hw_params_get_buffer_size(HwParams, &BufferSize));
91 
92  /* Check all is OK */
93  if (rrate != AlsaRate) CHECKERR(ErrAlsaHw);
94  #ifdef TALK
95  printf("Rate %u\n", rrate);
96  #endif
97 
98  CHECKERR(snd_pcm_hw_params_get_access(HwParams, &acces));
99  if (acces != AlsaAccessMode) CHECKERR(ErrAlsaHw);
100  #ifdef TALK
101  printf("Access %u\n", (unsigned int)acces);
102  #endif
103 
104  CHECKERR(snd_pcm_hw_params_get_format(HwParams, &format));
105  if (format != AlsaAccessFormat) CHECKERR(ErrAlsaHw);
106  #ifdef TALK
107  printf("Format %u\n", (unsigned int)format);
108  #endif
109 
110  CHECKERR(snd_pcm_hw_params_get_channels(HwParams, &tmp));
111  if (tmp != AlsaChannels) CHECKERR(ErrAlsaHw);
112  #ifdef TALK
113  printf("Channels number %u\n", tmp);
114  printf("Buffer Size %lu\n", BufferSize);
115  #endif
116 
117  /* Free the hardware parameters now */
118  snd_pcm_hw_params_free(HwParams);
119 
120  return BufferSize;
121  }
122 #endif
123 
124 int Read_WAVHeader (WAVHeader *, FILE *);
132 int Read_WAVHeader(WAVHeader *Header, FILE *fp)
133 {
134  unsigned char buffer[4];
135 
136  /* Read string RIFF */
137  if (fread(Header->riff, sizeof(Header->riff), 1, fp) != 1) return ErrReadFile;
138  #ifdef DUMP
139  printf("(01-04) riff string: %s\n", Header->riff);
140  #endif
141 
142  /* 1st Read overall size, 2nd convert little endian -> big endian (4 byte int) */
143  if (fread(buffer, 4, 1, fp) != 1) return ErrReadFile;
144  Header->size = buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24);
145  #ifdef DUMP
146  printf("(05-08) Overall file size %u bytes\n", Header->size);
147  #endif
148 
149  /* Read string WAVE */
150  if (fread(Header->wave, sizeof(Header->wave), 1, fp) != 1) return ErrReadFile;
151  #ifdef DUMP
152  printf("(09-12) wave string: %s\n", Header->wave);
153  #endif
154 
155  /* Read string FMT */
156  if (fread(Header->fmt, sizeof(Header->fmt), 1, fp) != 1) return ErrReadFile;
157  #ifdef DUMP
158  printf("(13-16) fmt string: %s\n", Header->fmt);
159  #endif
160 
161  /* 1st Read length of the format data, 2nd convert little endian -> big endian (4 byte int) */
162  if (fread(buffer, 4, 1, fp) != 1) return ErrReadFile;
163  Header->fmt_length = buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24);
164  #ifdef DUMP
165  printf("(17-20) Length of the format data: %u bytes\n", Header->fmt_length);
166  #endif
167  if (Header->fmt_length != 16)
168  {
169  printf("Fmt data length not equal to 16, not supported. End\n");
170  return ErrWavFormat;
171  }
172 
173  /* 1st Read format type, 2nd convert little endian -> big endian (4 byte int) */
174  if (fread(buffer, 2, 1, fp) != 1) return ErrReadFile;
175  Header->format_type = buffer[0] | (buffer[1] << 8);
176  #ifdef DUMP
177  printf("(21-22) Format type: %u (1 is PCM) \n", Header->format_type);
178  #endif
179  if (Header->format_type != 1)
180  {
181  printf("Format type not equal to PCM, not supported. End\n");
182  return ErrWavFormat;
183  }
184 
185  /* 1st Read number of channels, 2nd convert little endian -> big endian (4 byte int) */
186  if (fread(buffer, 2, 1, fp) != 1) return ErrReadFile;
187  Header->channels = buffer[0] | (buffer[1] << 8);
188  #ifdef DUMP
189  printf("(23-24) Number of channels: %u\n", Header->channels);
190  #endif
191  if (Header->channels != 1)
192  {
193  printf("Number of channels not equal to 1 (mono), not supported. End\n");
194  return ErrWavFormat;
195  }
196 
197  /* 1st Read sample_rate, 2nd convert little endian -> big endian (4 byte int) */
198  if (fread(buffer, 4, 1, fp) != 1) return ErrReadFile;
199  Header->sample_rate = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
200  #ifdef DUMP
201  printf("(25-28) Sample rate: %u\n", Header->sample_rate);
202  #endif
203 
204  /* 1st Read byte rate, 2nd convert little endian -> big endian (4 byte int) */
205  if (fread(buffer, 4, 1, fp) != 1) return ErrReadFile;
206  Header->byte_rate = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
207  #ifdef DUMP
208  printf("(29-32) Byte Rate: %u\n", Header->byte_rate);
209  #endif
210 
211  /* 1st Read block align, 2nd convert little endian -> big endian (4 byte int) */
212  if (fread(buffer, 2, 1, fp) != 1) return ErrReadFile;
213  Header->block_align = buffer[0] | (buffer[1] << 8);
214  #ifdef DUMP
215  printf("(33-34) Block Alignment: %u\n", Header->block_align);
216  #endif
217 
218  /* 1st Read bits per sample, 2nd convert little endian -> big endian (4 byte int) */
219  if (fread(buffer, 2, 1, fp) != 1) return ErrReadFile;
220  Header->bits_per_sample = buffer[0] | (buffer[1] << 8);
221  #ifdef DUMP
222  printf("(35-36) Bits per sample: %u\n", Header->bits_per_sample);
223  #endif
224  if (Header->byte_rate != (Header->sample_rate * Header->channels * Header->bits_per_sample / 8))
225  {
226  printf("byte rate is not equal to (sample_rate * channels * bits_per_sample), not supported. End\n");
227  return ErrWavFormat;
228  }
229 
230  /* Read string DATA */
231  /* Standard wav file header size is 44 bytes. Some audios have undocumented data */
232  /* into the header. The way to know if this is a not a 44 byte header is to check */
233  /* if strinf DATA is here */
234  if (fread(Header->data_header, sizeof(Header->data_header), 1, fp) != 1) return ErrReadFile;
235  #ifdef DUMP
236  printf("(37-40) String DATA: %s\n", Header->data_header);
237  #endif
238  if (strncasecmp("data", Header->data_header, 4) != 0)
239  {
240  printf("Not standard wav file header size (44 bytes), not supported. End\n");
241  return ErrWavFormat;
242  }
243 
244  /* 1st Read data size, 2nd convert little endian -> big endian (4 byte int) */
245  if (fread(buffer, 4, 1, fp) != 1) return ErrReadFile;
246  Header->data_size = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
247  #ifdef DUMP
248  printf("(41-44) size of data section is %u bytes\n", Header->data_size);
249  #endif
250 
251  Header->num_samples = Header->data_size / Header->block_align;
252  #ifdef DUMP
253  printf("Number of samples: %lu\n", Header->num_samples);
254  #endif
255  if (Header->num_samples != (8*Header->data_size) / (Header->channels*Header->bits_per_sample))
256  {
257  printf("(data_size / block_align) not equal to ((8*data_size) / (channels*bits_per_sample)). End\n");
258  return ErrWavFormat;
259  }
260 
261  Header->bytes_per_sample = (Header->bits_per_sample * Header->channels) / 8;
262  #ifdef DUMP
263  printf("Number of bytes per sample: %u\n", Header->bytes_per_sample);
264  #endif
265  if (Header->bytes_per_sample != 2)
266  {
267  printf("Byte per sample is not equal to 2. End\n");
268  return ErrWavFormat;
269  }
270 
271  return OK;
272 }
273 
274 bool DetectSilence(MyType, MyType *, MyType *);
283 bool DetectSilence(MyType obsprob, MyType *prob_silen, MyType *prob_audio)
284 {
285  MyType pss, pas, psa, paa, norm;
286 
287  #ifdef SIMPLE
288  obsprob = 1 / (1 + expf(-obsprob));
289  #else
290  obsprob = 1 / (1 + exp(-obsprob));
291  #endif
292 
293  /* HMM */
294  pss = (*prob_silen) * (MyType)HMMremain;
295  pas = (*prob_audio) * (MyType)HMMchange;
296  psa = (*prob_silen) * (MyType)HMMchange;
297  paa = (*prob_audio) * (MyType)HMMremain;
298 
299  if (pss > pas)
300  (*prob_silen) = pss * obsprob;
301  else
302  (*prob_silen) = pas * obsprob;
303 
304  if (psa > paa)
305  (*prob_audio) = psa * (1-obsprob);
306  else
307  (*prob_audio) = paa * (1-obsprob);
308 
309  norm = (*prob_silen) + (*prob_audio);
310 
311  (*prob_silen) = (*prob_silen) / norm;
312  (*prob_audio) = (*prob_audio) / norm;
313 
314  return (*prob_silen > *prob_audio);
315 }
316 
317 #endif
char riff[4]
Definition: defines.h:253
int Read_WAVHeader(WAVHeader *, FILE *)
Read_WAVHeader reads header of a WAVE file, checks its compability and fill Header struct...
char wave[4]
Definition: defines.h:255
char data_header[4]
Definition: defines.h:264
Struct for WAVE file header. WAV files definitions. Note that we only work with 1 channel...
Definition: defines.h:251
unsigned int format_type
Definition: defines.h:258
unsigned int byte_rate
Definition: defines.h:261
char SoundID[32]
Definition: defines.h:197
General header file with constants, defines, structs, etc. using by ReMAS, both CPU and GPU...
unsigned int fmt_length
Definition: defines.h:257
long num_samples
Definition: defines.h:266
bool DetectSilence(MyType, MyType *, MyType *)
DetectSilence checks whether audio (frame) is silence or audio.
unsigned int block_align
Definition: defines.h:262
unsigned int sample_rate
Definition: defines.h:260
unsigned int channels
Definition: defines.h:259
Struct for store global information of the problem. Each composition needs a file with values for th...
Definition: defines.h:190
unsigned int size
Definition: defines.h:254
char fmt[4]
Definition: defines.h:256
unsigned int bits_per_sample
Definition: defines.h:263
unsigned int data_size
Definition: defines.h:265
unsigned int bytes_per_sample
Definition: defines.h:267