-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudio.cpp
More file actions
54 lines (45 loc) · 1.23 KB
/
Audio.cpp
File metadata and controls
54 lines (45 loc) · 1.23 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
#include <cmath>
#include "Audio.hpp"
#define BUFFER_LEN 1024
Audio::Audio(char * filename)
{
int readcount;
SNDFILE *infile;
SF_INFO sfinfo;
if (! (infile = sf_open(filename, SFM_READ, &sfinfo)))
{
/* Open failed so print an error message. */
printf ("Not able to open input file %s.\n", filename) ;
/* Print the error message fron libsndfile. */
sf_perror (NULL) ;
return;
}
channels = sfinfo.channels;
data = (double **) malloc(sizeof(double *) * channels);
double * data_read = (double *) malloc(sizeof(double) * BUFFER_LEN * channels);
int total_samples = 0;
while ((readcount = sf_read_double (infile, data_read, BUFFER_LEN * channels)))
{
int samples_read = (readcount - (readcount % channels)) / channels;
for(int chan = 0; chan < channels; chan++)
{
data[chan] = (double *) realloc(data[chan], total_samples + samples_read);
for(int i = 0; i < samples_read; i++)
{
data[chan][i + total_samples] = data_read[i * channels + chan];
}
}
total_samples += samples_read;
}
length = total_samples;
}
Audio::Audio(double ** data_, int channels_, int length_)
{
data = data_;
channels = channels_;
length = length_;
}