-
Notifications
You must be signed in to change notification settings - Fork 45
DiskWriter
See <utilities/diskwriter.h>
DiskWriter provides a namespace with static methods allowing you to store recorded audio as .WAV files on the filesystem. DiskWriter can buffer fragments and delay the actual writing to disk until these buffers (known as snippets) are full (see Recording and bouncing engine output).
void prepare( std::string outputFilename, int chunkSize, int amountOfChannels )
Prepare for a new recording. Recordings can consist of multiple iterations (snippets), each of given chunkSize in buffer size. A recording can use a maximum of two snippet buffers during its lifetime. Whenever a snippet buffer is full, it should be written to disk and flushed so it can be used when the other buffer has filled up. As such you can reuse the two buffers continuously during a recording (e.g. until finish() is called). Ideally chunkSize is a multiple of the buffer size of the buffers provided to appendBuffer().
amountOfChannels specifies the channel amount of the recorded snippet (usually equal to AudioEngineProps::OUTPUT_CHANNELS when recording the engine output, or equal to amount AudioEngineProps::INPUT_CHANNELS when recording from the device inputs).
void finish()
Completes a recording. This will combine all files that have been written to temporary storage (listed in the outputFiles-vector) into a single .WAV file defined by outputFilename in the prepare() method. Upon completion, all temporary files will be deleted from storage and allocated buffer memory is freed. When writing the single file, each snippet is read and removed prior to writing its contents into the target file, this ensures that no extra storage space is required during this process.
void prepareSnippet()
Invoked by prepare() as well as the engine during recording/bouncing whenever a snippet has filled its buffer completely (by querying bufferFull()). This method will prepare the next snippet to continue recording audio into. When this is invoked, you can proceed to save the full snippet buffer onto disk as a temporary file so its contents can be freed for memory. This saving should happen in a separate thread to prevent buffer under runs from occurring while the engine continues to render audio while recording large fragments. Saving should however complete before the next snippet has filled its complete buffer to prevent data loss. In other words: do not call this method in succession without having called writeBufferToFile() in between.
void appendBuffer( AudioBuffer* aBuffer )
void appendBuffer( float* aBuffer, int aBufferSize, int amountOfChannels )
Appends the contents of given AudioBuffer into the current snippets buffer.
bool bufferFull()
Queries whether the current snippet buffer is full. If so, prepareSnippet() should be invoked so the other snippet buffer can be used to continue writing into (e.g. becomes the "current" buffer) while the filled snippet buffer can be saved onto disk in the meantime.
void writeBufferToFile( int bufferIndex, bool broadcastUpdate )
Writes the contents of the snippet buffer specified by bufferIndex onto disk as a temporary file. If this is invoked on a buffer that has not been filled completely, it will only write the written contents (and thus omit writing silence). This also frees memory allocated to the buffer (via flushOutput()). broadcastUpdate specifies whether to broadcast Notifications::RECORDED_SNIPPET_SAVED along with the index of the saved snippet (this is not the buffer index, but the index number of the amount of snippets rendered in chronological order). The temporary file path and size are saved in the outputFiles vector.
flushOutput( int bufferIndex )
Clears the snippet buffer at given bufferIndex and frees allocated memory.
int currentBufferIndex
Index of the snippet buffer currently in use for the recording.