<commit_before>use dsp::{Node, Settings};
use waveforms::{Waveform, Sine};

pub type Frequency = f64;
pub type Phase = f64;
pub type Output = f32; // f64 not supported yet
pub type Amp = f64;
pub type Volume = f64;

pub struct Oscillator {
  phase: Phase,
  waveform: Sine // TODO: any waveform
}

pub struct Synth {
  oscillator: Oscillator,
  frequency: Frequency,
  volume: Amp,
}

impl Oscillator {
    pub fn new(waveform: Sine) -> Oscillator {
        Oscillator {
            phase: 0.0,
            waveform: waveform
        }
    }

    pub fn amp_at_next_phase(&mut self, frequency: Frequency, settings: Settings) -> Amp {
        let val = self.waveform.amp_at_phase(self.phase);
        self.phase += frequency / settings.sample_hz as f64;
        val
    }
}

impl Synth {
    pub fn new(oscillator: Oscillator, frequency: Frequency, volume: Amp) -> Synth {
        Synth {
            oscillator: oscillator, // TODO: multiple voices
            frequency: frequency,  // TODO: dynamic frequency
            volume: volume  // TODO: dynamic volume
        }
    }


    fn volume(&self, amp: Amp) -> Volume {
        amp * self.volume
    }
}

impl Node<Output> for Synth {
    /// Here we'll override the audio_requested method and generate a sound.
    fn audio_requested(&mut self, buffer: &mut [Output], settings: Settings) {
        for frame in buffer.chunks_mut(settings.channels as usize) {
            let val = self.oscillator.amp_at_next_phase(self.frequency, settings);
            for channel in frame.iter_mut() {
                *channel = self.volume(val) as f32;
            }
        }
    }
}

pub fn sine_synth (frequency: Frequency) -> Synth {
    Synth::new(Oscillator::new(Sine), frequency, 0.15)
}<commit_msg>More simple<commit_after>use dsp::{Node, Settings};
use waveforms::{Waveform, Sine};

pub type Frequency = f64;
pub type Phase = f64;
pub type Output = f32; // f64 not supported yet
pub type Amp = f64;
pub type Volume = f64;

pub struct Oscillator {
  phase: Phase,
  waveform: Sine // TODO: any waveform
}

pub struct Synth {
  oscillator: Oscillator,
  frequency: Frequency,
  volume: Amp,
}

impl Oscillator {
    pub fn new(waveform: Sine) -> Oscillator {
        Oscillator {
            phase: 0.0,
            waveform: waveform
        }
    }

    pub fn amp_at_next_phase(&mut self, frequency: Frequency, settings: Settings) -> Amp {
        let val = self.waveform.amp_at_phase(self.phase);
        self.phase += frequency / settings.sample_hz as f64;
        val
    }
}

impl Synth {
    pub fn new(oscillator: Oscillator, frequency: Frequency, volume: Amp) -> Synth {
        Synth {
            oscillator: oscillator, // TODO: multiple oscillator
            frequency: frequency,  // TODO: dynamic frequency
            volume: volume  // TODO: dynamic volume
        }
    }
}

impl Node<Output> for Synth {
    /// Here we'll override the audio_requested method and generate a sound.
    fn audio_requested(&mut self, buffer: &mut [Output], settings: Settings) {
        for frame in buffer.chunks_mut(settings.channels as usize) {
            let amp = self.oscillator.amp_at_next_phase(self.frequency, settings);
            for channel in frame.iter_mut() {
                *channel = (amp * self.volume) as f32;
            }
        }
    }
}

pub fn sine_synth (frequency: Frequency) -> Synth {
    Synth::new(Oscillator::new(Sine), frequency, 0.15)
}
{'fraction_non_alphanumeric': 0.10572429906542057, 'fraction_numerical': 0.012266355140186916, 'mean_word_length': 2.4491440080563947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2072692', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1062, 'n_words': 319}