-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cd-reader.cpp
217 lines (166 loc) · 4.72 KB
/
cd-reader.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "cd-reader.h"
#include <climits>
static void* FileOpenCallback(const char* const filename, const ClownCD_FileMode mode)
{
const char *mode_string;
switch (mode)
{
case CLOWNCD_RB:
mode_string = "rb";
break;
case CLOWNCD_WB:
mode_string = "wb";
break;
default:
return nullptr;
}
return SDL_RWFromFile(filename, mode_string);
}
static int FileCloseCallback(void* const stream)
{
return SDL_RWclose(static_cast<SDL_RWops*>(stream));
}
static size_t FileReadCallback(void* const buffer, const std::size_t size, const std::size_t count, void* const stream)
{
return SDL_RWread(static_cast<SDL_RWops*>(stream), buffer, size, count);
}
static size_t FileWriteCallback(const void* const buffer, const std::size_t size, const std::size_t count, void* const stream)
{
return SDL_RWwrite(static_cast<SDL_RWops*>(stream), buffer, size, count);
}
static long FileTellCallback(void* const stream)
{
const auto position = SDL_RWtell(static_cast<SDL_RWops*>(stream));
if (position < LONG_MIN || position > LONG_MAX)
return -1L;
return position;
}
static int FileSeekCallback(void* const stream, const long position, const ClownCD_FileOrigin origin)
{
int whence;
switch (origin)
{
case CLOWNCD_SEEK_SET:
whence = RW_SEEK_SET;
break;
case CLOWNCD_SEEK_CUR:
whence = RW_SEEK_CUR;
break;
case CLOWNCD_SEEK_END:
whence = RW_SEEK_END;
break;
default:
return -1;
}
return SDL_RWseek(static_cast<SDL_RWops*>(stream), position, whence) == -1 ? -1 : 0;
}
static const ClownCD_FileCallbacks callbacks = {FileOpenCallback, FileCloseCallback, FileReadCallback, FileWriteCallback, FileTellCallback, FileSeekCallback};
void CDReader::Open(SDL::RWops &&stream, const std::filesystem::path &path)
{
// Transfer ownership of the stream to ClownCD.
clowncd.Open(ClownCD_OpenAlreadyOpen(stream.release(), path.string().c_str(), &callbacks));
audio_playing = false;
}
bool CDReader::SeekToSector(const SectorIndex sector_index)
{
if (!IsOpen())
return false;
const auto track_type = ClownCD_SeekTrackIndex(&clowncd.data, 1, 1);
if (track_type != CLOWNCD_CUE_TRACK_MODE1_2048 && track_type != CLOWNCD_CUE_TRACK_MODE1_2352)
return false;
return ClownCD_SeekSector(&clowncd.data, sector_index);
}
CDReader::Sector CDReader::ReadSector()
{
Sector sector;
if (!IsOpen())
{
sector.fill(0);
return sector;
}
ClownCD_ReadSector(&clowncd.data, sector.data());
return sector;
}
CDReader::Sector CDReader::ReadSector(const SectorIndex sector_index)
{
Sector sector;
if (!IsOpen())
{
sector.fill(0);
return sector;
}
SeekToSector(sector_index);
return ReadSector();
}
bool CDReader::PlayAudio(const TrackIndex track_index, const PlaybackSetting setting)
{
if (!IsOpen())
return false;
audio_playing = false;
if (ClownCD_SeekTrackIndex(&clowncd.data, track_index, 1) != CLOWNCD_CUE_TRACK_AUDIO)
return false;
audio_playing = true;
playback_setting = setting;
return true;
}
bool CDReader::SeekToFrame(const FrameIndex frame_index)
{
if (!ClownCD_SeekAudioFrame(&clowncd.data, frame_index))
{
audio_playing = false;
return false;
}
return true;
}
cc_u32f CDReader::ReadAudio(cc_s16l* const sample_buffer, const cc_u32f total_frames)
{
if (!IsOpen())
return 0;
if (!audio_playing)
return 0;
cc_u32f frames_read = 0;
while (frames_read != total_frames)
{
frames_read += ClownCD_ReadFrames(&clowncd.data, &sample_buffer[frames_read * 2], total_frames - frames_read);
if (frames_read != total_frames)
{
switch (playback_setting)
{
case PlaybackSetting::ALL:
if (!PlayAudio(clowncd.data.track.current_track + 1, playback_setting))
audio_playing = false;
break;
case PlaybackSetting::ONCE:
audio_playing = false;
// Fallthrough
case PlaybackSetting::REPEAT:
if (!SeekToFrame(0))
audio_playing = false;
break;
}
if (!audio_playing)
break;
}
}
return frames_read;
}
CDReader::State CDReader::GetState()
{
State state;
state.track_index = clowncd.data.track.current_track;
state.sector_index = clowncd.data.track.current_sector;
state.frame_index = clowncd.data.track.current_frame;
state.playback_setting = playback_setting;
state.audio_playing = audio_playing;
return state;
}
bool CDReader::SetState(const State &state)
{
if (!IsOpen())
return false;
if (ClownCD_SetState(&clowncd.data, state.track_index, 1, state.sector_index, state.frame_index) == CLOWNCD_CUE_TRACK_INVALID)
return false;
playback_setting = state.playback_setting;
audio_playing = state.audio_playing;
return true;
}