Skip to content

Commit d4adab6

Browse files
authored
Fixes wav files written with WavWriter.h to be complete (#663)
* Fixes wav files written with WavWriter.h to be complete Before the transfer buffer was not flushed so wav header files would say the file was a certain size but actually it would be smaller because the full data was not written to the wav file. This would result in perfect loops being recalled imperfectly. See https://github.com/willemOH/daisy_looper/tree/main commits for debugging which proves this behavior. * Improves reliability of multiple consecutive writes before, the first write would be exact, but subsequent writes would introduce artifacts or inaccuracies to the wav file as compared to recording into a buffer. Powercycling would solve this but not ideal. Now, after 5 writes, there is usually not artefacts or differences between wav file and equivalent buffer. Although occasionally this isn't true so this isn't a complete fix and the issue should be looked into by someone who knows more about this class and fatfs than I.
1 parent 785ebb5 commit d4adab6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/util/WavWriter.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,30 @@ class WavWriter
143143
{
144144
unsigned int bw = 0;
145145
recording_ = false;
146-
// We _should_ flush whatever's left in the transfer buff
147-
// TODO: that.
146+
147+
// Flush remaining data in the transfer buffer
148+
if (wptr_ > 0) // Check if there is unwritten data in the buffer
149+
{
150+
uint32_t remaining_size = wptr_ * (cfg_.bitspersample / 8);
151+
// Ensure remaining_size does not exceed the buffer size
152+
if (remaining_size > sizeof(transfer_buff))
153+
{
154+
remaining_size = sizeof(transfer_buff);
155+
}
156+
f_write(&fp_, transfer_buff, remaining_size, &bw);
157+
}
158+
148159
wavheader_.FileSize = CalcFileSize();
149160
f_lseek(&fp_, 0);
150161
f_write(&fp_, &wavheader_, sizeof(wavheader_), &bw);
151162
f_close(&fp_);
163+
164+
// Clear the transfer buffer and reset the buffer state
165+
memset(transfer_buff, 0, sizeof(transfer_buff));
166+
bstate_ = BufferState::IDLE;
167+
wptr_ = 0; // Reset the write pointer
168+
num_samps_ = 0; // Reset the number of samples
169+
recording_ = false; // Ensure recording is inactive
152170
}
153171

154172
/** Opens a file for writing. Writes the initial WAV Header, and gets ready for stream-based recording. */

0 commit comments

Comments
 (0)