blob: 8d203c63cf2de8b51c062593b718b247ac598883 [file] [log] [blame]
Bill Coxca02d872010-11-02 15:10:52 -04001/* Sonic library
2 Copyright 2010
3 Bill Cox
4 This file is part of the Sonic Library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
19
20/*
21This file supports read/write wave files.
22*/
23#include <stdlib.h>
24#include <limits.h>
Bill Cox0c4c0602010-11-08 11:46:30 -050025#include <string.h>
Bill Coxca02d872010-11-02 15:10:52 -040026#include <sndfile.h>
27#include "wave.h"
28
29struct waveFileStruct {
30 SNDFILE *soundFile;
31 short *values;
32 int numValues;
Bill Cox2081ea42010-11-05 05:49:47 -040033 int numChannels;
Bill Coxca02d872010-11-02 15:10:52 -040034};
35
36/* Open the file for reading. Also determine it's sample rate. */
37waveFile openInputWaveFile(
38 char *fileName,
39 int *sampleRate)
40{
41 SF_INFO info;
42 SNDFILE *soundFile;
43 waveFile file;
44
45 info.format = 0;
46 soundFile = sf_open(fileName, SFM_READ, &info);
47 if(soundFile == NULL) {
48 fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL));
49 return NULL;
50 }
51 file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
52 file->soundFile = soundFile;
Bill Cox2081ea42010-11-05 05:49:47 -040053 file->numChannels = info.channels;
Bill Coxca02d872010-11-02 15:10:52 -040054 file->numValues = 42;
55 file->values = (short *)calloc(file->numValues, sizeof(short));
56 *sampleRate = info.samplerate;
Bill Cox14efa442010-11-02 15:43:58 -040057 printf("Frames = %ld, sample rate = %d, channels = %d, format = %d\n",
Bill Coxca02d872010-11-02 15:10:52 -040058 info.frames, info.samplerate, info.channels, info.format);
59 return file;
60}
61
62/* Open the file for reading. */
63waveFile openOutputWaveFile(
64 char *fileName,
65 int sampleRate)
66{
67 SF_INFO info;
68 SNDFILE *soundFile;
69 waveFile file;
70
71 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
72 info.samplerate = sampleRate;
73 info.channels = 1;
74 soundFile = sf_open(fileName, SFM_WRITE, &info);
75 if(soundFile == NULL) {
76 fprintf(stderr, "Unable to open wave file %s: %s", fileName, sf_strerror(NULL));
77 return NULL;
78 }
79 file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
80 file->soundFile = soundFile;
81 file->numValues = 42;
82 file->values = (short *)calloc(file->numValues, sizeof(short));
83 return file;
84}
85
86/* Close the sound file. */
87void closeWaveFile(
88 waveFile file)
89{
90 SNDFILE *soundFile = file->soundFile;
91
92 sf_close(soundFile);
93}
94
95/* Read from the wave file. */
96int readFromWaveFile(
97 waveFile file,
Bill Cox0c4c0602010-11-08 11:46:30 -050098 short *buffer,
Bill Coxca02d872010-11-02 15:10:52 -040099 int maxSamples)
100{
101 SNDFILE *soundFile = file->soundFile;
Bill Cox2081ea42010-11-05 05:49:47 -0400102 float value;
103 short *values;
Bill Coxca02d872010-11-02 15:10:52 -0400104 int samplesRead;
Bill Cox2081ea42010-11-05 05:49:47 -0400105 int numChannels = file->numChannels;
106 int i, j;
Bill Coxca02d872010-11-02 15:10:52 -0400107
108 if(maxSamples > file->numValues) {
109 file->numValues = maxSamples;
110 file->values = (short *)realloc(file->values, maxSamples*sizeof(short));
111 }
Bill Cox2081ea42010-11-05 05:49:47 -0400112 values = file->values;
113 samplesRead = sf_read_short(soundFile, values, maxSamples);
Bill Coxca02d872010-11-02 15:10:52 -0400114 if(samplesRead <= 0) {
115 return 0;
116 }
Bill Cox2081ea42010-11-05 05:49:47 -0400117 samplesRead /= numChannels;
Bill Cox0c4c0602010-11-08 11:46:30 -0500118 if(numChannels > 1) {
119 for(i = 0; i < samplesRead; i++) {
120 value = 0.0f;
121 for(j = 0; j < numChannels; j++) {
122 value += values[i*numChannels + j];
123 }
124 buffer[i] = (short)(value/(numChannels*32768.0f) + 0.5);
Bill Cox2081ea42010-11-05 05:49:47 -0400125 }
Bill Cox0c4c0602010-11-08 11:46:30 -0500126 } else {
127 memcpy(buffer, values, samplesRead*sizeof(short));
Bill Coxca02d872010-11-02 15:10:52 -0400128 }
129 return samplesRead;
130}
131
132/* Write to the wave file. */
133int writeToWaveFile(
134 waveFile file,
Bill Cox0c4c0602010-11-08 11:46:30 -0500135 short *buffer,
Bill Coxca02d872010-11-02 15:10:52 -0400136 int numSamples)
137{
138 SNDFILE *soundFile = file->soundFile;
Bill Cox0c4c0602010-11-08 11:46:30 -0500139 int numWritten;
Bill Coxca02d872010-11-02 15:10:52 -0400140
141 if(numSamples > file->numValues) {
142 file->numValues = numSamples*3/2;
143 file->values = (short *)realloc(file->values, file->numValues*sizeof(short));
144 }
Bill Cox0c4c0602010-11-08 11:46:30 -0500145 numWritten = sf_write_short(soundFile, buffer, numSamples);
Bill Coxca02d872010-11-02 15:10:52 -0400146 if(numWritten != numSamples) {
147 fprintf(stderr, "Unable to write wave file.\n");
148 return 0;
149 }
150 return 1;
151}