looper/backends/playback/zsm/zsm_backend.hpp

198 lines
6 KiB
C++
Raw Normal View History

2024-08-08 13:12:37 -07:00
#pragma once
#include "playback_backend.hpp"
2024-10-14 21:27:16 -07:00
#include <omp.h>
#include "x16emu/ymglue.h"
2024-08-08 13:12:37 -07:00
#include <cstdint>
#include <stddef.h>
#include <stdint.h>
2024-10-14 21:27:16 -07:00
#include <stdlib.h>
#include <string.h>
#include <streambuf>
#include <vector>
#include <util.hpp>
#include <SDL.h>
extern "C" {
#include "x16emu/audio.h"
#include "x16emu/vera_pcm.h"
#include "x16emu/vera_psg.h"
#include "x16emu/ymglue.h"
}
2024-08-08 13:12:37 -07:00
#include "file_backend.hpp"
2024-10-14 21:27:16 -07:00
#define YM_FREQ (3579545/64)
#define PSG_FREQ (AUDIO_SAMPLERATE)
2024-08-08 13:12:37 -07:00
enum ZsmCommandId {
PsgWrite,
ExtCmd,
FmWrite,
ZsmEOF,
Delay
};
struct reg_pair {
uint8_t reg;
uint8_t val;
};
struct ZsmCommand {
ZsmCommandId id;
union {
struct {
uint8_t reg;
uint8_t val;
} psg_write;
struct {
uint8_t channel;
uint8_t bytes;
union {
uint8_t *pcm;
struct {
uint8_t chip_id;
uint8_t writes;
uint8_t *write_bytes;
} expansion;
uint8_t *sync;
uint8_t *custom;
};
} extcmd;
struct {
uint8_t len;
reg_pair *regs;
} fm_write;
uint8_t delay;
};
~ZsmCommand();
};
2024-10-16 13:06:40 -07:00
struct pcm_instrument {
uint8_t geom;
uint32_t loop_rem;
uint32_t loop;
bool islooped;
uint32_t remain;
uint8_t *data = nullptr;
inline ~pcm_instrument() {
if (data != nullptr) free((void*)data);
}
};
2024-08-08 13:12:37 -07:00
class ZsmBackend : public PlaybackBackend {
File *file;
2024-10-14 21:27:16 -07:00
Fifo<int16_t> audio_buf;
DynPtr psg_buf;
DynPtr pcm_buf;
DynPtr out_buf;
DynPtr ym_buf;
DynPtr ym_resample_buf;
bool ym_recorded = false;
uint8_t ym_data[256];
2024-10-14 21:27:16 -07:00
uint32_t loop_rem;
uint32_t pcm_data_offs;
2024-10-15 12:23:47 -07:00
uint8_t pcm_data_instruments;
2024-10-14 21:27:16 -07:00
uint32_t loop;
bool islooped;
uint32_t remain;
uint32_t cur;
uint32_t pcm_loop_point;
uint32_t rem_point;
SDL_AudioStream *fm_stream;
2024-10-16 13:06:40 -07:00
std::vector<pcm_instrument*> instruments;
uint8_t *audio_sample = nullptr;
bool pcm_enable();
bool psg_enable();
bool fm_enable();
2024-10-14 21:27:16 -07:00
int16_t combine_audio(int16_t a, int16_t b) {
return (int16_t)((((int32_t)a) + ((int32_t)b)) >> 1);
}
void audio_step(size_t samples) {
if (samples == 0) return;
2024-10-16 09:56:06 -07:00
while (remain != 0 && pcm_fifo_avail() < samples) {
2024-10-15 15:28:59 -07:00
if (pcm_read_rate() == 0) break;
2024-10-15 15:34:55 -07:00
if ((--remain) == 0) {
2024-10-15 15:28:59 -07:00
if (islooped) {
cur = loop;
2024-10-15 16:17:23 -07:00
remain = loop_rem;
2024-10-15 15:28:59 -07:00
} else {
break;
}
}
size_t oldpos = file->get_pos();
2024-10-16 13:06:40 -07:00
uint8_t sample = audio_sample[cur++];
2024-10-15 15:28:59 -07:00
pcm_write_fifo(sample);
}
2024-10-14 21:27:16 -07:00
samples *= 2;
int16_t *psg_ptr = psg_buf.get_item_sized<int16_t>(samples);
int16_t *pcm_ptr = pcm_buf.get_item_sized<int16_t>(samples);
psg_render(psg_ptr, samples / 2);
pcm_render(pcm_ptr, samples / 2);
int16_t *out_ptr = out_buf.get_item_sized<int16_t>(samples);
// The exact amount of samples needed for the stream.
double ratio = ((double)YM_FREQ) / ((double)PSG_FREQ);
size_t needed_samples = ((size_t)std::floor(samples * ratio)) / 2;
int16_t *ym_ptr = ym_buf.get_item_sized<int16_t>(needed_samples * 2);
YM_stream_update(ym_ptr, needed_samples);
assert(SDL_AudioStreamPut(fm_stream, ym_ptr, needed_samples * 2 * sizeof(int16_t)) == 0);
while (SDL_AudioStreamAvailable(fm_stream) < ((samples + 2) * sizeof(int16_t))) {
YM_stream_update(ym_ptr, 1);
assert(SDL_AudioStreamPut(fm_stream, ym_ptr, 2 * sizeof(int16_t)) == 0);
}
int16_t *ym_resample_ptr = ym_resample_buf.get_item_sized<int16_t>(samples);
ssize_t ym_resample_len = SDL_AudioStreamGet(fm_stream, ym_resample_ptr, (samples + 2) * sizeof(int16_t));
assert(ym_resample_len >= 0);
ym_resample_len /= sizeof(int16_t);
for (size_t i = 0; i < samples / 2; i++) {
size_t j = i * 2;
2024-10-17 10:28:22 -07:00
int16_t psg[2] = {psg_ptr[j] >> 1, psg_ptr[j + 1] >> 1};
int16_t pcm[2] = {pcm_ptr[j] >> 1, pcm_ptr[j + 1] >> 1};
if (!pcm_enable()) memset(pcm, 0, sizeof(pcm));
if (!psg_enable()) memset(psg, 0, sizeof(psg));
2024-10-17 10:28:22 -07:00
int16_t vera[2] = {psg[0] + pcm[0], psg[1] + pcm[1]};
int16_t fm[2] = {ym_resample_ptr[j], ym_resample_ptr[j + 1]};
if (!fm_enable()) memset(fm, 0, sizeof(fm));
2024-10-17 10:28:22 -07:00
int16_t mix[2] = {vera[0] + (fm[0] >> 1), vera[1] + (fm[1] >> 1)};
2024-10-14 21:27:16 -07:00
out_ptr[j++] = mix[0];
out_ptr[j++] = mix[1];
}
audio_buf.push(out_ptr, samples);
}
inline void *reserve(size_t len) {
return (void*)audio_buf.reserve(len);
}
inline size_t copy_out(void *buf, size_t len) {
return audio_buf.pop((int16_t*)buf, len);
}
2024-08-08 13:12:37 -07:00
uint32_t loop_point;
2024-10-14 21:27:16 -07:00
double loop_pos = 0.0;
2024-08-08 13:12:37 -07:00
uint32_t pcm_offset;
uint8_t fm_mask;
uint16_t psg_channel_mask;
uint16_t tick_rate;
size_t music_data_start;
size_t music_data_len;
double ticks;
2024-10-14 21:27:16 -07:00
ssize_t delayTicks = 0;
2024-08-08 13:12:37 -07:00
double position;
2024-10-14 21:27:16 -07:00
double cpuClocks = 0;
inline double get_delay_per_frame() {
return 1.0;
}
void tick(bool step = true);\
2024-10-14 21:27:16 -07:00
void seek_internal(double position, bool loop = true);
2024-08-08 13:12:37 -07:00
ZsmCommand get_command();
public:
2024-10-14 21:27:16 -07:00
uint64_t get_min_samples() override;
std::optional<uint64_t> get_max_samples() override;
2024-08-08 13:12:37 -07:00
inline std::string get_id() override {
return "zsm";
}
inline std::string get_name() override {
return "ZSM player";
}
void seek(double position) override;
void load(const char *filename) override;
void switch_stream(int idx) override;
void cleanup() override;
int get_stream_idx() override;
size_t render(void *buf, size_t maxlen) override;
double get_position() override;
2024-10-14 21:27:16 -07:00
inline double get_length() override {
return length;
}
2024-08-08 13:12:37 -07:00
inline ~ZsmBackend() override { }
};