libsidplayfp 2.15.0
demo.cpp
/*
* This file is part of libsidplayfp, a SID player engine.
*
* Copyright 2012-2023 Leandro Nini <drfiemost@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <fcntl.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <fstream>
#include <memory>
#include <vector>
#include <iostream>
#include <sidplayfp/sidplayfp.h>
#include <sidplayfp/SidTune.h>
#include <sidplayfp/SidInfo.h>
#include <sidplayfp/builders/residfp.h>
/*
* Adjust these paths to point to existing ROM dumps if needed.
*/
#define KERNAL_PATH ""
#define BASIC_PATH ""
#define CHARGEN_PATH ""
#define SAMPLERATE 48000
#if __cplusplus < 201103L
# define unique_ptr auto_ptr
#endif
/*
* Load ROM dump from file.
* Allocate the buffer if file exists, otherwise return 0.
*/
char* loadRom(const char* path, size_t romSize)
{
char* buffer = 0;
std::ifstream is(path, std::ios::binary);
if (is.good())
{
buffer = new char[romSize];
is.read(buffer, romSize);
}
is.close();
return buffer;
}
/*
* Sample application that shows how to use libsidplayfp
* to play a SID tune from a file.
* It uses OSS for audio output.
*/
int main(int, char* argv[])
{
sidplayfp m_engine;
{ // Load ROM files
char *kernal = loadRom(KERNAL_PATH, 8192);
char *basic = loadRom(BASIC_PATH, 8192);
char *chargen = loadRom(CHARGEN_PATH, 4096);
m_engine.setRoms((const uint8_t*)kernal, (const uint8_t*)basic, (const uint8_t*)chargen);
delete [] kernal;
delete [] basic;
delete [] chargen;
}
// Set up a SID builder
std::unique_ptr<ReSIDfpBuilder> rs(new ReSIDfpBuilder("Demo"));
// Get the number of SIDs supported by the engine
unsigned int maxsids = (m_engine.info ()).maxsids();
// Create SID emulators
rs->create(maxsids);
// Check if builder is ok
if (!rs->getStatus())
{
std::cerr << rs->error() << std::endl;
return -1;
}
// Load tune from file
std::unique_ptr<SidTune> tune(new SidTune(argv[1]));
// CHeck if the tune is valid
if (!tune->getStatus())
{
std::cerr << tune->statusString() << std::endl;
return -1;
}
// Select default song
tune->selectSong(0);
// Configure the engine
SidConfig cfg;
cfg.frequency = SAMPLERATE;
cfg.fastSampling = false;
cfg.sidEmulation = rs.get();
if (!m_engine.config(cfg))
{
std::cerr << m_engine.error() << std::endl;
return -1;
}
// Load tune into engine
if (!m_engine.load(tune.get()))
{
std::cerr << m_engine.error() << std::endl;
return -1;
}
// Setup audio device
int handle=::open("/dev/dsp", O_WRONLY, 0);
int format=AFMT_S16_LE;
ioctl(handle, SNDCTL_DSP_SETFMT, &format);
int chn=2;
ioctl(handle, SNDCTL_DSP_CHANNELS, &chn);
int sampleRate=SAMPLERATE;
ioctl(handle, SNDCTL_DSP_SPEED, &sampleRate);
int bufferSize;
ioctl(handle, SNDCTL_DSP_GETBLKSIZE, &bufferSize);
// 48000/~1000000 * 5000 * 2
short buffer[512];
// Play for ~5 seconds
m_engine.initMixer(true);
for (int i=0; i<1000; i++)
{
int res = m_engine.play(5000);
if (res < 0)
{
std::cerr << m_engine.error() << std::endl;
break;
}
unsigned int s = m_engine.mix(buffer, res);
::write(handle, buffer, s*sizeof(short));
}
::close(handle);
}
Definition residfp.h:33
Definition SidConfig.h:40
uint_least32_t frequency
Definition SidConfig.h:139
@ MONO
One channel mono playback.
Definition SidConfig.h:45
playback_t playback
Definition SidConfig.h:134
bool fastSampling
Definition SidConfig.h:179
@ INTERPOLATE
Interpolation.
Definition SidConfig.h:85
sidbuilder * sidEmulation
Definition SidConfig.h:153
sampling_method_t samplingMethod
Definition SidConfig.h:173
Definition SidTune.h:43
Definition sidplayfp.h:47
const SidInfo & info() const
Definition sidplayfp.cpp:84
const SidConfig & config() const
Definition sidplayfp.cpp:49
void initMixer(bool stereo)
Definition sidplayfp.cpp:155
bool load(SidTune *tune)
Definition sidplayfp.cpp:79
const char * error() const
Definition sidplayfp.cpp:99
unsigned int mix(short *buffer, unsigned int samples)
Definition sidplayfp.cpp:160
SID_DEPRECATED uint_least32_t play(short *buffer, uint_least32_t count)
Definition sidplayfp.cpp:59
void setRoms(const uint8_t *kernal, const uint8_t *basic=0, const uint8_t *character=0)
Definition sidplayfp.cpp:133