-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoadFile.h
62 lines (48 loc) · 1.62 KB
/
LoadFile.h
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
#include <stdio.h>
#include <sndfile.h>
#include <malloc.h>
#include "AudioObject.h"
struct AudioObject LoadFile(char * path)
{
struct AudioObject Object1;
// Open sound file
SF_INFO sndInfo;
SNDFILE *sndFile = sf_open(path, SFM_READ, &sndInfo);
if (sndFile == NULL) {
fprintf(stderr, "Error reading source file '%s': %s\n", path, sf_strerror(sndFile));
return Object1;
}
// Check format - 16bit PCM
if (sndInfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_16)) {
fprintf(stderr, "Input should be 16bit Wav\n");
sf_close(sndFile);
return Object1;
}
// Check channels - mono
if (sndInfo.channels != 1) {
fprintf(stderr, "Wrong number of channels\n");
sf_close(sndFile);
return Object1;
}
// Allocate memory
float *buffer = malloc(sndInfo.frames * sizeof(float));
if (buffer == NULL) {
fprintf(stderr, "Could not allocate memory for data\n");
sf_close(sndFile);
return Object1;
}
// Load data
long numFrames = sf_readf_float(sndFile, buffer, sndInfo.frames);
// Check correct number of samples loaded
if (numFrames != sndInfo.frames) {
fprintf(stderr, "Did not read enough frames for source\n");
sf_close(sndFile);
free(buffer);
return Object1;
}
// Preparation of the Audio Object to return
Object1.pos = 0;
Object1.stream= buffer;
Object1.size = sndInfo.frames;
return Object1;
}