Repository navigation
Expand file tree
/
Copy pathcppsid.cpp
More file actions
74 lines (62 loc) · 2.19 KB
/
Copy pathcppsid.cpp
File metadata and controls
74 lines (62 loc) · 2.19 KB
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
#include <fstream>
#include <SDL.h>
#include <cppsid.h>
extern "C" {
#include <libcsid.h>
}
namespace CPPSID {
void Player::sdl_callback(void* userdata, unsigned char *stream, int bytes) {
libcsid_render((unsigned short *)stream, bytes / 2);
}
Player::Player() {
VERBOSE = false;
is_playing = false;
samplerate = DEFAULT_SAMPLERATE;
sidmodel = SIDMODEL_8580;
if (SDL_Init(SDL_INIT_AUDIO) < 0)
throw std::runtime_error("error initializing SDL");
soundspec = std::make_shared<SDL_AudioSpec>(SDL_AudioSpec({0, }));
soundspec->freq = samplerate;
soundspec->channels = 1;
soundspec->format = AUDIO_S16;
soundspec->samples = 32768;
soundspec->userdata = nullptr;
soundspec->callback = sdl_callback;
sdl_device = SDL_OpenAudioDevice(nullptr, 0, soundspec.get(), nullptr, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
if (not sdl_device)
throw std::runtime_error("could not open SDL audio");
libcsid_init(samplerate, sidmodel);
}
bool Player::load(const std::string &file, int subtune) {
stop();
buffer.clear();
std::ifstream f(file.c_str(), std::ifstream::ate | std::ios::binary);
if (f) {
buffer.resize(f.tellg()); // resize buffer (moves f forward)
f.seekg(0, f.beg); // go back to beginning of file
f.read((char*)buffer.data(), buffer.size()); // load sid into buffer
libcsid_load(buffer.data(), buffer.size(), subtune);
if (not buffer.empty())
return true;
}
return false;
}
std::string Player::author() { return libcsid_getauthor(); }
std::string Player::title() { return libcsid_gettitle(); }
std::string Player::info() { return libcsid_getinfo(); }
void Player::start() {
if (not buffer.empty()) {
SDL_PauseAudioDevice(sdl_device, 0);
is_playing=true;
}
}
void Player::stop() {
if (is_playing)
SDL_PauseAudioDevice(sdl_device, 1);
is_playing=false;
}
Player::~Player() {
stop();
SDL_CloseAudioDevice(sdl_device);
}
}//namespace