blob: ef869fa4436bafa371467c431795f64cd51fc1ce [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
Bill Coxa9999872010-11-11 14:36:59 -05006 The Sonic Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
Bill Coxca02d872010-11-02 15:10:52 -040010
Bill Coxa9999872010-11-11 14:36:59 -050011 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
Bill Coxca02d872010-11-02 15:10:52 -040015
Bill Coxa9999872010-11-11 14:36:59 -050016 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
Bill Coxca02d872010-11-02 15:10:52 -040020
21/*
22This file supports read/write wave files.
23*/
24#include <stdlib.h>
25#include <limits.h>
Bill Cox0c4c0602010-11-08 11:46:30 -050026#include <string.h>
Bill Coxca02d872010-11-02 15:10:52 -040027#include <sndfile.h>
28#include "wave.h"
29
30struct waveFileStruct {
31 SNDFILE *soundFile;
32 short *values;
33 int numValues;
Bill Cox2081ea42010-11-05 05:49:47 -040034 int numChannels;
Bill Coxca02d872010-11-02 15:10:52 -040035};
36
37/* Open the file for reading. Also determine it's sample rate. */
38waveFile openInputWaveFile(
39 char *fileName,
40 int *sampleRate)
41{
42 SF_INFO info;
43 SNDFILE *soundFile;
44 waveFile file;
45
46 info.format = 0;
47 soundFile = sf_open(fileName, SFM_READ, &info);
48 if(soundFile == NULL) {
49 fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL));
50 return NULL;
51 }
52 file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
53 file->soundFile = soundFile;
Bill Cox2081ea42010-11-05 05:49:47 -040054 file->numChannels = info.channels;
Bill Coxca02d872010-11-02 15:10:52 -040055 file->numValues = 42;
56 file->values = (short *)calloc(file->numValues, sizeof(short));
57 *sampleRate = info.samplerate;
Bill Cox14efa442010-11-02 15:43:58 -040058 printf("Frames = %ld, sample rate = %d, channels = %d, format = %d\n",
Bill Coxca02d872010-11-02 15:10:52 -040059 info.frames, info.samplerate, info.channels, info.format);
60 return file;
61}
62
63/* Open the file for reading. */
64waveFile openOutputWaveFile(
65 char *fileName,
66 int sampleRate)
67{
68 SF_INFO info;
69 SNDFILE *soundFile;
70 waveFile file;
71
72 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
73 info.samplerate = sampleRate;
74 info.channels = 1;
75 soundFile = sf_open(fileName, SFM_WRITE, &info);
76 if(soundFile == NULL) {
Bill Cox0c4cade2010-11-09 05:54:54 -050077 fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL));
Bill Coxca02d872010-11-02 15:10:52 -040078 return NULL;
79 }
80 file = (waveFile)calloc(1, sizeof(struct waveFileStruct));
81 file->soundFile = soundFile;
82 file->numValues = 42;
83 file->values = (short *)calloc(file->numValues, sizeof(short));
84 return file;
85}
86
87/* Close the sound file. */
88void closeWaveFile(
89 waveFile file)
90{
91 SNDFILE *soundFile = file->soundFile;
92
93 sf_close(soundFile);
94}
95
96/* Read from the wave file. */
97int readFromWaveFile(
98 waveFile file,
Bill Cox0c4c0602010-11-08 11:46:30 -050099 short *buffer,
Bill Coxca02d872010-11-02 15:10:52 -0400100 int maxSamples)
101{
102 SNDFILE *soundFile = file->soundFile;
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500103 int value;
Bill Cox2081ea42010-11-05 05:49:47 -0400104 short *values;
Bill Coxca02d872010-11-02 15:10:52 -0400105 int samplesRead;
Bill Cox2081ea42010-11-05 05:49:47 -0400106 int numChannels = file->numChannels;
107 int i, j;
Bill Coxca02d872010-11-02 15:10:52 -0400108
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500109 if(maxSamples*numChannels > file->numValues) {
110 file->numValues = maxSamples*numChannels;
111 file->values = (short *)realloc(file->values, file->numValues*sizeof(short));
Bill Coxca02d872010-11-02 15:10:52 -0400112 }
Bill Cox2081ea42010-11-05 05:49:47 -0400113 values = file->values;
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500114 samplesRead = sf_read_short(soundFile, values, maxSamples*numChannels);
Bill Coxca02d872010-11-02 15:10:52 -0400115 if(samplesRead <= 0) {
116 return 0;
117 }
Bill Cox2081ea42010-11-05 05:49:47 -0400118 samplesRead /= numChannels;
Bill Cox0c4c0602010-11-08 11:46:30 -0500119 if(numChannels > 1) {
120 for(i = 0; i < samplesRead; i++) {
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500121 value = 0;
Bill Cox0c4c0602010-11-08 11:46:30 -0500122 for(j = 0; j < numChannels; j++) {
123 value += values[i*numChannels + j];
124 }
Bill Cox0e4ec5e2010-11-09 13:26:40 -0500125 if(value >= 0) {
126 buffer[i] = value/numChannels;
127 } else {
128 /* On some OSes, dividing a negative number rounds the wrong way */
129 buffer[i] = -(-value/numChannels);
130 }
Bill Cox2081ea42010-11-05 05:49:47 -0400131 }
Bill Cox0c4c0602010-11-08 11:46:30 -0500132 } else {
133 memcpy(buffer, values, samplesRead*sizeof(short));
Bill Coxca02d872010-11-02 15:10:52 -0400134 }
135 return samplesRead;
136}
137
138/* Write to the wave file. */
139int writeToWaveFile(
140 waveFile file,
Bill Cox0c4c0602010-11-08 11:46:30 -0500141 short *buffer,
Bill Coxca02d872010-11-02 15:10:52 -0400142 int numSamples)
143{
144 SNDFILE *soundFile = file->soundFile;
Bill Cox0c4c0602010-11-08 11:46:30 -0500145 int numWritten;
Bill Coxca02d872010-11-02 15:10:52 -0400146
147 if(numSamples > file->numValues) {
148 file->numValues = numSamples*3/2;
149 file->values = (short *)realloc(file->values, file->numValues*sizeof(short));
150 }
Bill Cox0c4c0602010-11-08 11:46:30 -0500151 numWritten = sf_write_short(soundFile, buffer, numSamples);
Bill Coxca02d872010-11-02 15:10:52 -0400152 if(numWritten != numSamples) {
153 fprintf(stderr, "Unable to write wave file.\n");
154 return 0;
155 }
156 return 1;
157}