How can i do to playback ASIO with less latency and do a Signal Graph at the same time?

31 Views Asked by At

I'm trying to do a simple program, to playback the audio of the input of my focusrite and make a wave of this input.

Also, I want to hear with the less latency the input of my audio interface.

This is the code i have so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.Wave.Asio;
using System.Drawing;
using System.Drawing.Imaging;

namespace TG
{
    public partial class Prueba_de_ASIO : Form
    {
        private AsioOut asioOut;
        private string selectedAsioDriverName;

        public Prueba_de_ASIO(string asioDriverName)
        {
            InitializeComponent();
            selectedAsioDriverName = asioDriverName;
            InitializeAsio();
        }

        private void InitializeAsio()
        {
            try
            {
                asioOut = new AsioOut(selectedAsioDriverName);
                asioOut.InitRecordAndPlayback(null, 2, 44100); // Ajusta los parámetros según sea necesario
                asioOut.AudioAvailable += OnAsioAudioAvailable;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al inicializar ASIO: " + ex.Message);
            }
        }

        private void OnAsioAudioAvailable(object sender, AsioAudioAvailableEventArgs e)
        {
            // Calcula el tamaño total del buffer necesario para todas las muestras en todos los canales
            int totalSamples = e.SamplesPerBuffer * e.InputBuffers.Length;
            float[] audioSamples = new float[totalSamples];
            var outputChannels = asioOut.DriverOutputChannelCount;


            // Llena el buffer con muestras de audio
            e.GetAsInterleavedSamples(audioSamples);

            // Procesa y dibuja la forma de onda
            DrawWaveform(audioSamples);
        }

        private void btnStartMonitoring_Click(object sender, EventArgs e)
        {
            try
            {
                if (asioOut != null)
                {
                    asioOut.Play();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al iniciar el monitoreo: " + ex.Message);
            }
        }

        private void btnStopMonitoring_Click(object sender, EventArgs e)
        {
            if (asioOut != null)
            {
                asioOut.Stop();
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (asioOut != null)
            {
                asioOut.Dispose();
            }
        }
        private void DrawWaveform(float[] audioSamples)
        {
            // Crear un bitmap para dibujar la forma de onda
            Bitmap waveformBitmap = new Bitmap(pictureBoxWaveform.Width, pictureBoxWaveform.Height);

            using (Graphics g = Graphics.FromImage(waveformBitmap))
            {
                g.Clear(Color.Black); // Fondo negro
                int midY = waveformBitmap.Height / 2;
                float prevX = 0;
                float prevY = midY;

                // Escalar los valores de la muestra para que se ajusten a la altura del PictureBox
                for (int i = 0; i < audioSamples.Length; i++)
                {
                    float x = (float)i / audioSamples.Length * waveformBitmap.Width;
                    float y = midY - audioSamples[i] * midY;

                    g.DrawLine(Pens.White, prevX, prevY, x, y);

                    prevX = x;
                    prevY = y;
                }
            }

            // Mostrar la imagen en el PictureBox
            pictureBoxWaveform.Image = waveformBitmap;
        }
    }
}

The program works fine because I see the wave in the screen but I cannot hear the input in my audio interface (Monitoring).

If anyone can help me that would be very usefull.

Thanks everybody

I tried this method NAudio: Using ASIO to record audio and output with a guitar

But it didn't work.

0

There are 0 best solutions below