template <bool enabled = true>
class WavWriter
Defined at line 47 of file ../../src/media/audio/lib/wav/wav_writer.h
This class enables a client to easily create and write LPCM audio data to a
RIFF-based WAV file. After creating the WavWriter object, Initialize should
be called before invoking other methods. If nullptr or "" is passed to
Initialize (instead of a valid file name), a default file path+name of
'/tmp/wav_writer_N.wav' is used, where N is an integer corresponding to the
instance of WavWriter running in that process.
Following Initialize, the Write method is used to instruct the library to
append the specified number of bytes to the audio file that has been created.
Once the client has completely written the file, the client should call Close
to update 'length' fields in the file and close the file. If the client
wishes, it can also occasionally call UpdateHeader, to update the 'length'
fields prior to file closure. These calls help maximize the amount of audio
data retained, in case of a crash before file closure, but at the expense of
higher file I/O load.
The method Reset discards any previously-written audio data, and returns the
file to a state of readiness to be provided audio data. By contrast, the
Delete method removes the file entirely -- subsequently the object would
generally be destroyed, although it can be revived by re-calling Initialize.
Note: this library makes no effort to be thread-safe, so clients bear all
responsibilities for synchronization.
Public Methods
bool Initialize (const char *const file_name, fuchsia::media::AudioSampleFormat sample_format, uint16_t channel_count, uint32_t frame_rate, uint16_t bits_per_sample)
Create the audio file; save the RIFF chunk and 'fmt ' / 'data' sub-chunks.
If this object already had a file open, the header is not updated.
TODO(mpuryear): leverage utility code elsewhere for bytes-per-sample lookup,
for either FIDL-defined sample types and/or driver defined sample packings.
Defined at line 150 of file ../../src/media/audio/lib/wav/wav_writer.cc
bool Write (void *const buffer, uint32_t num_bytes)
Write audio data to the file. This assumes that SEEK_SET is at end of file.
This can be called repeatedly without updating the header's length fields, if
desired. To update the header, the caller should also invoke UpdateHeader().
Defined at line 202 of file ../../src/media/audio/lib/wav/wav_writer.cc
bool UpdateHeader ()
We've previously written audio data to the file, so update the length fields.
This method need not write the entire header -- only the two length fields.
Defined at line 257 of file ../../src/media/audio/lib/wav/wav_writer.cc
bool Reset ()
Discard all previously written audio data, and return the WAV file to an
empty (but ready to be written) state. Reclaim file space as possible.
Defined at line 275 of file ../../src/media/audio/lib/wav/wav_writer.cc
bool Close ()
Finalize the file (update lengths in headers), and reset our file handle.
Any subsequent file updates will fail (although Delete can still succeed).
Defined at line 300 of file ../../src/media/audio/lib/wav/wav_writer.cc
bool Delete ()
Eliminate the WAV file (even if we've already closed it).
Defined at line 318 of file ../../src/media/audio/lib/wav/wav_writer.cc