blob: 1b5cd008ae6f2fc9056ef3e6beb6e2d0fea42f84 [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) {
Bill Cox0c4cade2010-11-09 05:54:54 -050076 fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL));
Bill Coxca02d872010-11-02 15:10:52 -040077 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 Cox0e4ec5e2010-11-09 13:26:40 -0500102 int value;
Bill Cox2081ea42010-11-05 05:49:47 -0400103 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
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500108 if(maxSamples*numChannels > file->numValues) {
109 file->numValues = maxSamples*numChannels;
110 file->values = (short *)realloc(file->values, file->numValues*sizeof(short));
Bill Coxca02d872010-11-02 15:10:52 -0400111 }
Bill Cox2081ea42010-11-05 05:49:47 -0400112 values = file->values;
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500113 samplesRead = sf_read_short(soundFile, values, maxSamples*numChannels);
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++) {
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500120 value = 0;
Bill Cox0c4c0602010-11-08 11:46:30 -0500121 for(j = 0; j < numChannels; j++) {
122 value += values[i*numChannels + j];
123 }
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500124 if(value >= 0) {
125 buffer[i] = value/numChannels;
126 } else {
127 /* On some OSes, dividing a negative number rounds the wrong way */
128 buffer[i] = -(-value/numChannels);
129 }
Bill Cox2081ea42010-11-05 05:49:47 -0400130 }
Bill Cox0c4c0602010-11-08 11:46:30 -0500131 } else {
132 memcpy(buffer, values, samplesRead*sizeof(short));
Bill Coxca02d872010-11-02 15:10:52 -0400133 }
134 return samplesRead;
135}
136
137/* Write to the wave file. */
138int writeToWaveFile(
139 waveFile file,
Bill Cox0c4c0602010-11-08 11:46:30 -0500140 short *buffer,
Bill Coxca02d872010-11-02 15:10:52 -0400141 int numSamples)
142{
143 SNDFILE *soundFile = file->soundFile;
Bill Cox0c4c0602010-11-08 11:46:30 -0500144 int numWritten;
Bill Coxca02d872010-11-02 15:10:52 -0400145
146 if(numSamples > file->numValues) {
147 file->numValues = numSamples*3/2;
148 file->values = (short *)realloc(file->values, file->numValues*sizeof(short));
149 }
Bill Cox0c4c0602010-11-08 11:46:30 -0500150 numWritten = sf_write_short(soundFile, buffer, numSamples);
Bill Coxca02d872010-11-02 15:10:52 -0400151 if(numWritten != numSamples) {
152 fprintf(stderr, "Unable to write wave file.\n");
153 return 0;
154 }
155 return 1;
156}