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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! Provides C compatible functions for working with this library as a DLL.

use crate::{ChipState, ChipParameters, Command, ChipGenerationData};

/// Initiates a new LSynth chip
#[no_mangle]
pub extern "C" fn ls_init(channel_count: usize, samplerate: usize, amplitude: f32, tick_rate: f32) -> *mut ChipState {
	Box::into_raw(Box::new(ChipState::new(channel_count, ChipParameters::new(samplerate, amplitude, tick_rate))))
}

/// Generates audio with the provided chip.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
/// 
/// buffer_ptr must point to the first f32 in an array, and buffer_len must be the length of that array.
#[no_mangle]
pub unsafe extern "C" fn ls_generate(chip_state: *mut ChipState, buffer_ptr: *mut f32, buffer_len: usize, buffer_start: usize) -> ChipGenerationData {
	let chip_state = &mut *chip_state;
	let buffer = std::slice::from_raw_parts_mut(buffer_ptr, buffer_len);
	
	chip_state.generate(&mut buffer[buffer_start..]).unwrap()
}

/// Inserts a command into the provided chip_state
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_send_command(chip_state: *mut ChipState, command: Command, channel: usize) {
	let chip_state = &mut *chip_state;
	
	let _ = chip_state.send_command(command, channel);
}

/// Returns the number of samples that are in a single tick.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_get_tick_frames(chip_state: *mut ChipState) -> f32 {
	let chip_state = & *chip_state;
	chip_state.parameters.get_tick_frames()
}

/// Sends a SetWaveform command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_set_waveform(chip_state: *mut ChipState, channel: usize, waveform: usize) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::SetWaveform(waveform), channel);
}

/// Sends a SetFrequency command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_set_frequency(chip_state: *mut ChipState, channel: usize, frequency: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::SetFrequency(frequency), channel);
}

/// Sends a SetAmplitude command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_set_amplitude(chip_state: *mut ChipState, channel: usize, amplitude: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::SetAmplitude(amplitude), channel);
}

/// Sends a SetPanning command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_set_panning(chip_state: *mut ChipState, channel: usize, panning: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::SetPanning(panning), channel);
}

/// Sends a SetCustomWaveform command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
/// waveform_ptr must point to the first f32 in an array, and waveform_len must be the length of that array.
#[no_mangle]
pub unsafe extern "C" fn ls_set_custom_waveform(chip_state: *mut ChipState, channel: usize, waveform_ptr: *mut f32, waveform_len: usize) {
	let chip_state = &mut *chip_state;
	
	let mut waveform = [0.0; crate::waveform::CUSTOM_WIDTH];
	waveform.clone_from_slice(std::slice::from_raw_parts(waveform_ptr, waveform_len));
	let _ = chip_state.send_command(Command::SetCustomWaveform(waveform), channel);
}

/// Sends a SetPhase command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_set_phase(chip_state: *mut ChipState, channel: usize, phase: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::SetPhase(phase), channel);
}

/// Sends a ForceSetAmplitude command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_force_set_amplitude(chip_state: *mut ChipState, channel: usize, amplitude: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::ForceSetAmplitude(amplitude), channel);
}

/// Sends a ForceSetPanning command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_force_set_panning(chip_state: *mut ChipState, channel: usize, panning: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::ForceSetPanning(panning), channel);
}

/// Sends a FrequencySlide command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_frequency_slide(chip_state: *mut ChipState, channel: usize, frequency: f32, rate: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::FrequencySlide(frequency, rate), channel);
}

/// Sends an AmplitudeSlide command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_amplitude_slide(chip_state: *mut ChipState, channel: usize, amplitude: f32, rate: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::AmplitudeSlide(amplitude, rate), channel);
}

/// Sends a PanningSlide command to the given channel.
/// # Safety
/// chip_state must be a valid ChipState generated from the ls_init function.
#[no_mangle]
pub unsafe extern "C" fn ls_panning_slide(chip_state: *mut ChipState, channel: usize, panning: f32, rate: f32) {
	let chip_state = &mut *chip_state;
	let _ = chip_state.send_command(Command::PanningSlide(panning, rate), channel);
}