-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcd-reader.h
122 lines (108 loc) · 3.28 KB
/
cd-reader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef CD_READER_H
#define CD_READER_H
#include <array>
#include <cstddef>
#include <filesystem>
#include "sdl-wrapper.h"
#include "common/cd-reader.h"
class CDReader
{
public:
enum class PlaybackSetting
{
ALL = CDREADER_PLAYBACK_ALL,
ONCE = CDREADER_PLAYBACK_ONCE,
REPEAT = CDREADER_PLAYBACK_REPEAT
};
using SectorIndex = CDReader_SectorIndex;
using TrackIndex = CDReader_TrackIndex;
using FrameIndex = CDReader_FrameIndex;
private:
CDReader_State state;
static void* FileOpenCallback(const char *filename, ClownCD_FileMode mode);
static int FileCloseCallback(void *stream);
static std::size_t FileReadCallback(void *buffer, std::size_t size, std::size_t count, void *stream);
static std::size_t FileWriteCallback(const void *buffer, std::size_t size, std::size_t count, void *stream);
static long FileTellCallback(void *stream);
static int FileSeekCallback(void *stream, long position, ClownCD_FileOrigin origin);
static constexpr ClownCD_FileCallbacks callbacks = {FileOpenCallback, FileCloseCallback, FileReadCallback, FileWriteCallback, FileTellCallback, FileSeekCallback};
public:
using State = CDReader_StateBackup;
static constexpr cc_u16f SECTOR_SIZE = CDREADER_SECTOR_SIZE;
CDReader()
{
CDReader_Initialise(&state);
}
~CDReader()
{
CDReader_Deinitialise(&state);
}
CDReader(const CDReader &other) = delete;
CDReader(CDReader &&other) = delete;
CDReader& operator=(const CDReader &other) = delete;
CDReader& operator=(CDReader &&other) = delete;
void Open(void* const stream, const std::filesystem::path &path)
{
CDReader_Open(&state, stream, reinterpret_cast<const char*>(path.u8string().c_str()), &callbacks);
}
void Open(SDL::IOStream &&stream, const std::filesystem::path &path)
{
// Transfer ownership of the stream to ClownCD.
Open(stream.release(), path);
}
void Close()
{
CDReader_Close(&state);
}
bool IsOpen() const
{
return CDReader_IsOpen(&state);
}
bool SeekToSector(const SectorIndex sector_index)
{
return CDReader_SeekToSector(&state, sector_index);
}
bool SeekToFrame(const FrameIndex frame_index)
{
return CDReader_SeekToFrame(&state, frame_index);
}
void ReadSector(cc_u16l* const buffer)
{
CDReader_ReadSector(&state, buffer);
}
bool PlayAudio(const TrackIndex track_index, const PlaybackSetting setting)
{
return CDReader_PlayAudio(&state, track_index, static_cast<CDReader_PlaybackSetting>(setting));
}
cc_u32f ReadAudio(cc_s16l* const sample_buffer, const cc_u32f total_frames)
{
return CDReader_ReadAudio(&state, sample_buffer, total_frames);
}
State GetState()
{
CDReader_StateBackup backup;
CDReader_GetStateBackup(&state, &backup);
return backup;
}
bool SetState(const State &state)
{
return CDReader_SetStateBackup(&this->state, &state);
}
bool ReadMegaCDHeaderSector(unsigned char* const buffer)
{
return CDReader_ReadMegaCDHeaderSector(&state, buffer);
}
bool IsMegaCDGame()
{
return CDReader_IsMegaCDGame(&state);
}
static bool IsMegaCDGame(const std::filesystem::path &path)
{
CDReader cd_reader;
cd_reader.Open(nullptr, path);
const bool is_mega_cd_game = cd_reader.IsMegaCDGame();
cd_reader.Close();
return is_mega_cd_game;
}
};
#endif /* CD_READER_H */