looper/backends/playback/zsm/zsm_backend.hpp

151 lines
3.6 KiB
C++
Raw Permalink 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-24 11:33:08 -07:00
Fifo<reg_pair> ym_pairs;
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-19 10:19:08 -07:00
double pcm_volume();
double psg_volume();
double fm_volume();
void audio_step(size_t samples);
2024-10-14 21:27:16 -07:00
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";
}
2024-11-12 14:53:44 -08:00
void add_licenses() override;
2024-10-19 10:19:08 -07:00
std::vector<Property> get_property_list() override;
2024-08-08 13:12:37 -07:00
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 { }
};