Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 1 | /* Sonic library |
| 2 | Copyright 2010 |
| 3 | Bill Cox |
| 4 | This file is part of the Sonic Library. |
| 5 | |
Bill Cox | a999987 | 2010-11-11 14:36:59 -0500 | [diff] [blame^] | 6 | 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 Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 10 | |
Bill Cox | a999987 | 2010-11-11 14:36:59 -0500 | [diff] [blame^] | 11 | 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 Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 15 | |
Bill Cox | a999987 | 2010-11-11 14:36:59 -0500 | [diff] [blame^] | 16 | 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 Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | This file supports read/write wave files. |
| 23 | */ |
| 24 | #include <stdlib.h> |
| 25 | #include <limits.h> |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 26 | #include <string.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 27 | #include <sndfile.h> |
| 28 | #include "wave.h" |
| 29 | |
| 30 | struct waveFileStruct { |
| 31 | SNDFILE *soundFile; |
| 32 | short *values; |
| 33 | int numValues; |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 34 | int numChannels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /* Open the file for reading. Also determine it's sample rate. */ |
| 38 | waveFile 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 Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 54 | file->numChannels = info.channels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 55 | file->numValues = 42; |
| 56 | file->values = (short *)calloc(file->numValues, sizeof(short)); |
| 57 | *sampleRate = info.samplerate; |
Bill Cox | 14efa44 | 2010-11-02 15:43:58 -0400 | [diff] [blame] | 58 | printf("Frames = %ld, sample rate = %d, channels = %d, format = %d\n", |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 59 | info.frames, info.samplerate, info.channels, info.format); |
| 60 | return file; |
| 61 | } |
| 62 | |
| 63 | /* Open the file for reading. */ |
| 64 | waveFile 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 Cox | 0c4cade | 2010-11-09 05:54:54 -0500 | [diff] [blame] | 77 | fprintf(stderr, "Unable to open wave file %s: %s\n", fileName, sf_strerror(NULL)); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 78 | 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. */ |
| 88 | void closeWaveFile( |
| 89 | waveFile file) |
| 90 | { |
| 91 | SNDFILE *soundFile = file->soundFile; |
| 92 | |
| 93 | sf_close(soundFile); |
| 94 | } |
| 95 | |
| 96 | /* Read from the wave file. */ |
| 97 | int readFromWaveFile( |
| 98 | waveFile file, |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 99 | short *buffer, |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 100 | int maxSamples) |
| 101 | { |
| 102 | SNDFILE *soundFile = file->soundFile; |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 103 | int value; |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 104 | short *values; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 105 | int samplesRead; |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 106 | int numChannels = file->numChannels; |
| 107 | int i, j; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 108 | |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 109 | if(maxSamples*numChannels > file->numValues) { |
| 110 | file->numValues = maxSamples*numChannels; |
| 111 | file->values = (short *)realloc(file->values, file->numValues*sizeof(short)); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 112 | } |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 113 | values = file->values; |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 114 | samplesRead = sf_read_short(soundFile, values, maxSamples*numChannels); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 115 | if(samplesRead <= 0) { |
| 116 | return 0; |
| 117 | } |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 118 | samplesRead /= numChannels; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 119 | if(numChannels > 1) { |
| 120 | for(i = 0; i < samplesRead; i++) { |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 121 | value = 0; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 122 | for(j = 0; j < numChannels; j++) { |
| 123 | value += values[i*numChannels + j]; |
| 124 | } |
Bill Cox | 0e4ec5e | 2010-11-09 13:26:40 -0500 | [diff] [blame] | 125 | 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 Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 131 | } |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 132 | } else { |
| 133 | memcpy(buffer, values, samplesRead*sizeof(short)); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 134 | } |
| 135 | return samplesRead; |
| 136 | } |
| 137 | |
| 138 | /* Write to the wave file. */ |
| 139 | int writeToWaveFile( |
| 140 | waveFile file, |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 141 | short *buffer, |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 142 | int numSamples) |
| 143 | { |
| 144 | SNDFILE *soundFile = file->soundFile; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 145 | int numWritten; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 146 | |
| 147 | if(numSamples > file->numValues) { |
| 148 | file->numValues = numSamples*3/2; |
| 149 | file->values = (short *)realloc(file->values, file->numValues*sizeof(short)); |
| 150 | } |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 151 | numWritten = sf_write_short(soundFile, buffer, numSamples); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 152 | if(numWritten != numSamples) { |
| 153 | fprintf(stderr, "Unable to write wave file.\n"); |
| 154 | return 0; |
| 155 | } |
| 156 | return 1; |
| 157 | } |