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 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program 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 |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 19 | |
| 20 | /* |
| 21 | This file supports read/write wave files. |
| 22 | */ |
| 23 | #include <stdlib.h> |
| 24 | #include <limits.h> |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 25 | #include <string.h> |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 26 | #include <sndfile.h> |
| 27 | #include "wave.h" |
| 28 | |
| 29 | struct waveFileStruct { |
| 30 | SNDFILE *soundFile; |
| 31 | short *values; |
| 32 | int numValues; |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 33 | int numChannels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* Open the file for reading. Also determine it's sample rate. */ |
| 37 | waveFile 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 Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 53 | file->numChannels = info.channels; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 54 | file->numValues = 42; |
| 55 | file->values = (short *)calloc(file->numValues, sizeof(short)); |
| 56 | *sampleRate = info.samplerate; |
Bill Cox | 14efa44 | 2010-11-02 15:43:58 -0400 | [diff] [blame] | 57 | printf("Frames = %ld, sample rate = %d, channels = %d, format = %d\n", |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 58 | info.frames, info.samplerate, info.channels, info.format); |
| 59 | return file; |
| 60 | } |
| 61 | |
| 62 | /* Open the file for reading. */ |
| 63 | waveFile 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. */ |
| 87 | void closeWaveFile( |
| 88 | waveFile file) |
| 89 | { |
| 90 | SNDFILE *soundFile = file->soundFile; |
| 91 | |
| 92 | sf_close(soundFile); |
| 93 | } |
| 94 | |
| 95 | /* Read from the wave file. */ |
| 96 | int readFromWaveFile( |
| 97 | waveFile file, |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 98 | short *buffer, |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 99 | int maxSamples) |
| 100 | { |
| 101 | SNDFILE *soundFile = file->soundFile; |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 102 | float value; |
| 103 | short *values; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 104 | int samplesRead; |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 105 | int numChannels = file->numChannels; |
| 106 | int i, j; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 107 | |
| 108 | if(maxSamples > file->numValues) { |
| 109 | file->numValues = maxSamples; |
| 110 | file->values = (short *)realloc(file->values, maxSamples*sizeof(short)); |
| 111 | } |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 112 | values = file->values; |
| 113 | samplesRead = sf_read_short(soundFile, values, maxSamples); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 114 | if(samplesRead <= 0) { |
| 115 | return 0; |
| 116 | } |
Bill Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 117 | samplesRead /= numChannels; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 118 | 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 Cox | 2081ea4 | 2010-11-05 05:49:47 -0400 | [diff] [blame] | 125 | } |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 126 | } else { |
| 127 | memcpy(buffer, values, samplesRead*sizeof(short)); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 128 | } |
| 129 | return samplesRead; |
| 130 | } |
| 131 | |
| 132 | /* Write to the wave file. */ |
| 133 | int writeToWaveFile( |
| 134 | waveFile file, |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 135 | short *buffer, |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 136 | int numSamples) |
| 137 | { |
| 138 | SNDFILE *soundFile = file->soundFile; |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 139 | int numWritten; |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 140 | |
| 141 | if(numSamples > file->numValues) { |
| 142 | file->numValues = numSamples*3/2; |
| 143 | file->values = (short *)realloc(file->values, file->numValues*sizeof(short)); |
| 144 | } |
Bill Cox | 0c4c060 | 2010-11-08 11:46:30 -0500 | [diff] [blame] | 145 | numWritten = sf_write_short(soundFile, buffer, numSamples); |
Bill Cox | ca02d87 | 2010-11-02 15:10:52 -0400 | [diff] [blame] | 146 | if(numWritten != numSamples) { |
| 147 | fprintf(stderr, "Unable to write wave file.\n"); |
| 148 | return 0; |
| 149 | } |
| 150 | return 1; |
| 151 | } |