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
|
/* Sonic library
Copyright 2010
Bill Cox
This file is part of the Sonic Library.
The Sonic Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
/*
This file supports read/write wave files.
*/
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <sndfile.h>
#include "wave.h"
struct waveFileStruct {
SNDFILE *soundFile;
int numChannels;
};
/* Open the file for reading. Also determine it's sample rate. */
waveFile openInputWaveFile(
char *fileName,
int *sampleRate,
int *numChannels)
{
SF_INFO info;
SNDFILE *soundFile;
waveFile file;
info.format = 0;
soundFile = sf_open(fileName, SFM_READ, &info);
if(soundFile == NULL) {
fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL));
return NULL;
}
file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
file->soundFile = soundFile;
file->numChannels = info.channels;
*sampleRate = info.samplerate;
*numChannels = info.channels;
printf("Frames = %ld, sample rate = %d, channels = %d, format = %d\n",
info.frames, info.samplerate, info.channels, info.format);
return file;
}
/* Open the file for reading. */
waveFile openOutputWaveFile(
char *fileName,
int sampleRate,
int numChannels)
{
SF_INFO info;
SNDFILE *soundFile;
waveFile file;
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
info.samplerate = sampleRate;
info.channels = numChannels;
soundFile = sf_open(fileName, SFM_WRITE, &info);
if(soundFile == NULL) {
fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL));
return NULL;
}
file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
file->soundFile = soundFile;
file->numChannels = numChannels;
return file;
}
/* Close the sound file. */
void closeWaveFile(
waveFile file)
{
SNDFILE *soundFile = file->soundFile;
sf_close(soundFile);
free(file);
}
/* Read from the wave file. */
int readFromWaveFile(
waveFile file,
short *buffer,
int maxSamples)
{
SNDFILE *soundFile = file->soundFile;
int samplesRead;
int numChannels = file->numChannels;
samplesRead = sf_read_short(soundFile, buffer, maxSamples*numChannels);
if(samplesRead <= 0) {
return 0;
}
return samplesRead/numChannels;
}
/* Write to the wave file. */
int writeToWaveFile(
waveFile file,
short *buffer,
int numSamples)
{
SNDFILE *soundFile = file->soundFile;
int numWritten;
numWritten = sf_write_short(soundFile, buffer, numSamples*file->numChannels);
if(numWritten != numSamples*file->numChannels) {
fprintf(stderr, "Unable to write wave file.\n");
return 0;
}
return 1;
}
|