Files
factorygame/util/audio.h
2025-06-02 22:49:53 +02:00

85 lines
1.7 KiB
C

/*
// Created by bruno on 16.2.2025.
*/
#define SAMPLE_RATE 44100
#define NUM_SYNTH_VOICES 128
#define SMOOTHING_FACTOR 0.001f
#define MIDI_TRACK_MAX 16
#define MAX_MIDI_EVENTS 1024
#define MIDI_VOICES MIDI_TRACK_MAX
#ifndef RISCB_AUDIO_H
#define RISCB_AUDIO_H
#include <SDL2/SDL.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "../tiles/tile.h"
typedef enum {
MIDI_NOTE_OFF = 0x80,
MIDI_NOTE_ON = 0x90,
MIDI_PROGRAM_CHANGE = 0xC0,
// You could add more here if needed
} MidiEventType;
typedef struct {
float timeSec; // When to trigger this event
MidiEventType type; // 0 = Note On, 1 = Note Off
uint8_t note;
uint8_t velocity;
} MidiEvent;
extern MidiEvent midiEvents[MIDI_TRACK_MAX][MAX_MIDI_EVENTS];
extern int midiEventCount[MIDI_TRACK_MAX];
extern int nextMidiEvent[MIDI_TRACK_MAX];
typedef enum Waveform {
WAVE_SINE,
WAVE_SQUARE,
WAVE_SAWTOOTH,
WAVE_TRIANGLE,
WAVE_NOISE,
WAVE_HALF_SINE,
WAVE_PULSE25,
WAVE_PULSE10,
WAVE_CLIPPED_SINE,
WAVE_EXP,
WAVE_RAMP,
WAVE_REVERSE_SAW,
WAVE_STAIRCASE,
WAVE_COUNT
} Waveform;
typedef struct SynthVoice {
Waveform waveform;
double phase;
uint16_t frequency;
uint8_t volume;
MiniRect sourceRect;
float smoothedAmp; // a float that holds the exponentially smoothed amplitude
} SynthVoice;
typedef struct AudioData {
SynthVoice synthVoices[NUM_SYNTH_VOICES];
SDL_Rect *playerRect;
float maxPanDistance;
uint64_t totalSamples;
} AudioData;
extern AudioData audioData;
void audio_callback(void *userdata, Uint8 *stream, int len);
void load_midi_file(const char *path);
uint16_t getAvailableChannel();
Waveform resolvePatch(uint8_t patchNum);
#endif //RISCB_AUDIO_H