Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

streaming audio output stuttering #188

Open
Frank-Buss opened this issue May 10, 2024 · 0 comments
Open

streaming audio output stuttering #188

Frank-Buss opened this issue May 10, 2024 · 0 comments

Comments

@Frank-Buss
Copy link

This code should create a sine wave, but stutters (tested on Linux) :

use raylib::{ffi::SetAudioStreamBufferSizeDefault, prelude::*};
use std::f32::consts::PI;
use raylib::prelude::RaylibAudio;

fn main() {
    let (mut rl, thread) = raylib::init()
        .size(800, 450)
        .title("raylib example - raw audio streaming")
        .build();

    rl.set_target_fps(30);
    unsafe { SetAudioStreamBufferSizeDefault(4096) };
    let mut audio = RaylibAudio::init_audio_device();
    let sample_rate = 44100;
    let sample_size = 16;
    let channels = 1;
    let mut audio_stream = AudioStream::init_audio_stream(&thread, sample_rate, sample_size, channels);
    audio.play_audio_stream(&mut audio_stream);
    let mut frequency = 440.0;
    let mut sine_wave = vec![0i16; 1024];
    let mut gt = 0;
    while !rl.window_should_close() {
        if audio.is_audio_stream_processed(&audio_stream) {
            audio_stream.update_audio_stream(&mut sine_wave);
            for sample in sine_wave.iter_mut() {
                let t = gt as f32 / sample_rate as f32;
                gt += 1;
                *sample = ((t * frequency * 2.0 * PI).sin() * i16::MAX as f32) as i16;
            }
        }
        if rl.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON) {
            let mouse_position = rl.get_mouse_position();
            frequency = 40.0 + mouse_position.y / 450.0 * 880.0;  // Modify frequency based on mouse Y position
        }
        let mut d = rl.begin_drawing(&thread);
        d.clear_background(Color::RAYWHITE);
        d.draw_text(&format!("Frequency: {:.2} Hz", frequency), 10, 20, 20, Color::DARKGRAY);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant