audiobackend: fixed issue where audio would be appended rather

than overwritten on some platforms

Despite the fact we don't actually do any reading here, using
QFile::WriteOnly on its own will truncate the file to 0 bytes which is
undesirable. The documentation says QFile::ReadOnly, Append or NewOnly
will prevent this. NewOnly won't work and reads are unnecessary, so
Append was used initially. However on some platforms, Append will _only_
allow writing at the end of the file (ignoring the seek() function)
meaning bytes won't be written where they're meant to be (this behavior
happens on Linux and not on Windows, the platform discrepancy is likely
a Qt bug). Using ReadWrite instead, despite not reading anything,
prevents truncation and allows for writing not at the end of the file.
This commit is contained in:
itsmattkc
2020-02-17 11:17:21 +11:00
parent 21755c7f20
commit 33a85aa059
+6 -4
View File
@@ -66,13 +66,15 @@ void AudioBackend::ThreadCompletedCache(NodeDependency dep, NodeValueTable data,
int out_point = offset + length;
QFile f(CachePathName());
if (f.open(QFile::WriteOnly | QFile::Append)) {
if (f.open(QFile::ReadWrite)) {
if (f.size() < out_point) {
f.resize(out_point);
if (f.size() < out_point && !f.resize(out_point)) {
qCritical() << "Failed to resize file" << CachePathName();
}
f.seek(offset);
if (!f.seek(offset)) {
qCritical() << "Failed to seek file" << CachePathName();
}
// Replace data with this data
int copy_length = qMin(length, cached_samples.size());