Skip to content

Commit c0277db

Browse files
committed
WAVReader: Make ReadFrames() return optional
That way the caller can detect EOF.
1 parent 132ca44 commit c0277db

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/util/cd_image_cue.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,11 @@ bool WaveTrackFileInterface::Read(void* buffer, u64 offset, u32 size, Error* err
524524
const u32 num_frames_to_read = std::min(num_frames, m_reader.GetNumFrames() - frame_number);
525525
if (num_frames_to_read > 0)
526526
{
527-
if (!m_reader.SeekToFrame(frame_number, error) || !m_reader.ReadFrames(buffer, num_frames_to_read, error))
527+
if (!m_reader.SeekToFrame(frame_number, error) ||
528+
m_reader.ReadFrames(buffer, num_frames_to_read, error).value_or(0) != num_frames_to_read)
529+
{
528530
return false;
531+
}
529532
}
530533

531534
// Padding.

src/util/wav_reader_writer.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ WAVReader::WAVReader(WAVReader&& move)
6464
{
6565
m_file = std::exchange(move.m_file, nullptr);
6666
m_frames_start = std::exchange(move.m_frames_start, 0);
67+
m_bytes_per_frame = std::exchange(move.m_bytes_per_frame, 0);
6768
m_sample_rate = std::exchange(move.m_sample_rate, 0);
6869
m_num_channels = std::exchange(move.m_num_channels, 0);
6970
m_num_frames = std::exchange(move.m_num_frames, 0);
@@ -79,6 +80,7 @@ WAVReader& WAVReader::operator=(WAVReader&& move)
7980
{
8081
m_file = std::exchange(move.m_file, nullptr);
8182
m_frames_start = std::exchange(move.m_frames_start, 0);
83+
m_bytes_per_frame = std::exchange(move.m_bytes_per_frame, 0);
8284
m_sample_rate = std::exchange(move.m_sample_rate, 0);
8385
m_num_channels = std::exchange(move.m_num_channels, 0);
8486
m_num_frames = std::exchange(move.m_num_frames, 0);
@@ -182,6 +184,7 @@ bool WAVReader::Open(const char* path, Error* error /*= nullptr*/)
182184
m_file = fp.release();
183185
m_frames_start = FileSystem::FTell64(m_file);
184186
m_sample_rate = format.sample_rate;
187+
m_bytes_per_frame = sizeof(s16) * format.num_channels;
185188
m_num_channels = format.num_channels;
186189
m_num_frames = num_frames;
187190
return true;
@@ -194,7 +197,9 @@ void WAVReader::Close()
194197

195198
std::fclose(m_file);
196199
m_file = nullptr;
200+
m_frames_start = 0;
197201
m_sample_rate = 0;
202+
m_bytes_per_frame = 0;
198203
m_num_channels = 0;
199204
m_num_frames = 0;
200205
}
@@ -204,6 +209,7 @@ std::FILE* WAVReader::TakeFile()
204209
std::FILE* ret = std::exchange(m_file, nullptr);
205210
m_sample_rate = 0;
206211
m_frames_start = 0;
212+
m_bytes_per_frame = 0;
207213
m_num_channels = 0;
208214
m_num_frames = 0;
209215
return ret;
@@ -220,15 +226,19 @@ bool WAVReader::SeekToFrame(u32 num, Error* error)
220226
return FileSystem::FSeek64(m_file, offset, SEEK_SET, error);
221227
}
222228

223-
bool WAVReader::ReadFrames(void* samples, u32 num_frames, Error* error /*= nullptr*/)
229+
std::optional<u32> WAVReader::ReadFrames(void* samples, u32 num_frames, Error* error /*= nullptr*/)
224230
{
225-
if (std::fread(samples, sizeof(s16) * m_num_channels, num_frames, m_file) != num_frames)
231+
const size_t read = std::fread(samples, m_bytes_per_frame, num_frames, m_file);
232+
if (read == 0)
226233
{
227-
Error::SetErrno(error, "fread() failed: ", errno);
228-
return false;
234+
if (std::ferror(m_file))
235+
{
236+
Error::SetErrno(error, "fread() failed: ", errno);
237+
return std::nullopt;
238+
}
229239
}
230240

231-
return true;
241+
return static_cast<u32>(read);
232242
}
233243

234244
WAVWriter::WAVWriter() = default;

src/util/wav_reader_writer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "common/types.h"
77

88
#include <cstdio>
9+
#include <optional>
910

1011
class Error;
1112

@@ -23,6 +24,7 @@ class WAVReader
2324
ALWAYS_INLINE u32 GetSampleRate() const { return m_sample_rate; }
2425
ALWAYS_INLINE u32 GetNumChannels() const { return m_num_channels; }
2526
ALWAYS_INLINE u32 GetNumFrames() const { return m_num_frames; }
27+
ALWAYS_INLINE u32 GetBytesPerFrame() const { return m_bytes_per_frame; }
2628
ALWAYS_INLINE u64 GetFramesStartOffset() const { return m_frames_start; }
2729
ALWAYS_INLINE bool IsOpen() const { return (m_file != nullptr); }
2830

@@ -34,13 +36,14 @@ class WAVReader
3436

3537
bool SeekToFrame(u32 num, Error* error = nullptr);
3638

37-
bool ReadFrames(void* samples, u32 num_frames, Error* error = nullptr);
39+
std::optional<u32> ReadFrames(void* samples, u32 num_frames, Error* error = nullptr);
3840

3941
private:
4042
using SampleType = s16;
4143

4244
std::FILE* m_file = nullptr;
4345
s64 m_frames_start = 0;
46+
u32 m_bytes_per_frame = 0;
4447
u32 m_sample_rate = 0;
4548
u32 m_num_channels = 0;
4649
u32 m_num_frames = 0;

0 commit comments

Comments
 (0)