Beat music detection

Hello,

Can you share with me the C# code which let me to the following thing…

I have music track saved i.e. mp3 or wav

and I would like to detect a beat in this music track and save time in miliseconds where this beat occurred into text file.

I would like to use TensorFlow AI library to do it.

I implemented the solution but not using AI and it is not so accurate as I would wish for.

Can you help me?
Regards
Marcin

Hi @Marcin_Zmyslowski_ph Welcome to The Tensorflow Forum,


using Magenta.Models.OnsetsFramesTranscription;
using NAudio.Wave;
using System;
using System.IO;
using System.Linq;
using TensorFlow;

namespace BeatDetection
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the audio file
            string audioPath = "path/to/your/audio.mp3";
            using (var audioFileReader = new AudioFileReader(audioPath))
            {
                // Decode the audio
                var audio = audioFileReader.ReadToEnd();
                var audioSampleRate = audioFileReader.WaveFormat.SampleRate;

                // Infer onsets and frames
                var  audioSampleRate);

                // Extract beat times
                var beatTimes = onsetsFrames.Onsets;

                // Save beat times to a text file
                File.WriteAllLines("beat_times.txt", beatTimes.Select(t => (t * 1000).ToString("0.00")));
            }
        }

        static OnsetsFramesOutputs InferOnsetsFrames(byte[] audio, int sampleRate)
        {
            // Initialize TensorFlow session
            using (var session = new Session())
            {
                // Load the OnsetsFramesTranscription model
                var model = new OnsetsFramesTranscription(session);

                // Create input tensor
                var inputTensor = Tensor.Create(audio);

                // Run the model
                var outputs = model.Outputs(inputTensor);

                // Return the onsets and frames
                return outputs;
            }
        }
    }
}
  • This code uses the NAudio.Wave library to read the audio file.
  • The InferOnsetsFrames() method uses the Magenta.Models.OnsetsFramesTranscription library to infer onsets and frames from the audio.
  • The beatTimes variable stores the time (in seconds) of each detected beat. This is then converted to milliseconds and written to a text file.

Thanks !