65 yCTrace(SOUNDFILE_WAV,
"bool PcmWavHeader::parse_from_file(FILE *fp)\n");
71 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
77 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
83 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
89 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
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");
144 yCError(SOUNDFILE_WAV,
"extra_size = %d\n", extra_size);
148 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
156 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
164 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
171 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
176 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
184 yCError(SOUNDFILE_WAV,
"failed to read .wav file");
193 int bitsPerSample = 16;
196 int align = channels*((bitsPerSample+7)/8);
204 pcm.pcmFormatTag = 1;
205 pcm.pcmChannels = channels;
207 pcm.pcmBytesPerSecond = align*
pcm.pcmSamplesPerSecond;
208 pcm.pcmBlockAlign = align;
209 pcm.pcmBitsPerSample = bitsPerSample;
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);
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...
void clear()
Disassociate object with any data block (deleting block if appropriate).
void allocate(size_t len)
Makes a data block of the specified length that will be deleted if this object is destroyed.
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.