58 void setup_to_write(
const Sound& sound, FILE *fp);
59 bool parse_from_file(FILE *fp);
65 yCTrace(SOUNDFILE_WAV,
"bool PcmWavHeader::parse_from_file(FILE *fp)\n");
69 ret = fread(&wavHeader,
sizeof(wavHeader), 1, fp);
71 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
75 ret = fread(&wavLength,
sizeof(wavLength), 1, fp);
77 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
81 ret = fread(&formatHeader1,
sizeof(formatHeader1), 1, fp);
83 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
87 ret = fread(&formatHeader2,
sizeof(formatHeader2), 1, fp);
89 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
93 ret = fread(&formatLength,
sizeof(formatLength), 1, fp);
95 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
99 ret = fread(&pcm.pcmFormatTag,
sizeof(pcm.pcmFormatTag), 1, fp);
101 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
105 ret = fread(&pcm.pcmChannels,
sizeof(pcm.pcmChannels), 1, fp);
107 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
111 ret = fread(&pcm.pcmSamplesPerSecond,
sizeof(pcm.pcmSamplesPerSecond), 1, fp);
113 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
117 ret = fread(&pcm.pcmBytesPerSecond,
sizeof(pcm.pcmBytesPerSecond), 1, fp);
119 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
123 ret = fread(&pcm.pcmBlockAlign,
sizeof(pcm.pcmBlockAlign), 1, fp);
125 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
129 ret = fread(&pcm.pcmBitsPerSample,
sizeof(pcm.pcmBitsPerSample), 1, fp);
131 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
134 if (pcm.pcmBitsPerSample != 16)
136 yCError(SOUNDFILE_WAV,
"sorry, lousy .wav read code only does 16-bit ints\n");
141 int extra_size = formatLength -
sizeof(pcm);
144 yCError(SOUNDFILE_WAV,
"extra_size = %d\n", extra_size);
145 pcmExtraData.allocate(extra_size);
146 ret = fread(&pcmExtraData, extra_size, 1, fp);
148 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
154 ret = fread(&dummyHeader,
sizeof(dummyHeader), 1, fp);
156 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
162 ret = fread(&dummyLength,
sizeof(dummyLength), 1, fp);
164 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
168 dummyData.allocate(dummyLength);
169 ret = fread(&dummyData, dummyLength, 1, fp);
171 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
174 ret = fread(&dummyHeader,
sizeof(dummyHeader), 1, fp);
176 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
181 dataHeader = dummyHeader;
182 ret = fread(&dataLength,
sizeof(dataLength), 1, fp);
184 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
193 int bitsPerSample = 16;
196 int align = channels*((bitsPerSample+7)/8);
202 formatLength =
sizeof(pcm);
204 pcm.pcmFormatTag = 1;
205 pcm.pcmChannels = channels;
207 pcm.pcmBytesPerSecond = align*pcm.pcmSamplesPerSecond;
208 pcm.pcmBlockAlign = align;
209 pcm.pcmBitsPerSample = bitsPerSample;
215 fwrite(&wavHeader,
sizeof(wavHeader),1,fp);
216 fwrite(&wavLength,
sizeof(wavLength),1,fp);
217 fwrite(&formatHeader1,
sizeof(formatHeader1),1,fp);
219 fwrite(&formatHeader2,
sizeof(formatHeader2),1,fp);
220 fwrite(&formatLength,
sizeof(formatLength),1,fp);
222 fwrite(&pcm.pcmFormatTag,
sizeof(pcm.pcmFormatTag),1,fp);
223 fwrite(&pcm.pcmChannels,
sizeof(pcm.pcmChannels),1,fp);
224 fwrite(&pcm.pcmSamplesPerSecond,
sizeof(pcm.pcmSamplesPerSecond),1,fp);
225 fwrite(&pcm.pcmBytesPerSecond,
sizeof(pcm.pcmBytesPerSecond),1,fp);
226 fwrite(&pcm.pcmBlockAlign,
sizeof(pcm.pcmBlockAlign),1,fp);
227 fwrite(&pcm.pcmBitsPerSample,
sizeof(pcm.pcmBitsPerSample),1,fp);
229 fwrite(&dataHeader,
sizeof(dataHeader),1,fp);
230 fwrite(&dataLength,
sizeof(dataLength),1,fp);
238 FILE *fp = fopen(filename,
"rb");
240 yCError(SOUNDFILE_WAV,
"cannot open file %s for reading\n", filename);
247 yCError(SOUNDFILE_WAV,
"error parsing header of file %s\n", filename);
260 yCDebug(SOUNDFILE_WAV,
"%d channels %d samples %d frequency\n", channels,
samples, freq);
263 result = fread(bytes.
get(),bytes.
length(),1,fp);
266 auto* data =
reinterpret_cast<NetInt16*
>(bytes.
get());
268 for (
int i=0; i<
samples; i++) {
269 for (
int j=0; j<channels; j++) {
270 sound_data.
set(data[ct],i,j);
281 yCError(SOUNDFILE_WAV,
"read_wav_bytestream() Not yet implemented");
287 FILE *fp = fopen(filename,
"wb");
289 yCError(SOUNDFILE_WAV,
"cannot open file %s for writing\n", filename);
297 auto* data =
reinterpret_cast<NetInt16*
>(bytes.
get());
301 for (
size_t i=0; i<
samples; i++) {
302 for (
size_t j=0; j<channels; j++) {
303 int v = sound_data.
get(i,j);
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
void setFrequency(int freq)
Set the frequency of the sound (i.e.
size_t getChannels() const
Get the number of channels of the sound.
void resize(size_t samples, size_t channels=1)
Set the sound size.
int getFrequency() const
Get the frequency of the sound (i.e.
audio_sample get(size_t sample, size_t channel=0) const
void set(audio_sample value, size_t sample, size_t channel=0)
size_t getSamples() const
Get the number of samples contained in the sound.
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface to the operating system, including Port based communication.
std::int16_t NetInt16
Definition of the NetInt16 type.
std::int32_t NetInt32
Definition of the NetInt32 type.
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
bool read_wav_file(Sound &data, const char *filename)
Read a sound from a .wav audio file.
bool write_wav_file(const Sound &data, const char *filename)
Write a sound to a .wav file.
bool read_wav_bytestream(Sound &data, const char *bytestream)
Read a sound from a byte array.
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.