Posts

Add Post

« Return to Posts

Unleash the Melodic Magic: Experience Music Beyond Imagination with AI Music Generator!

Unleash the Melodic Magic: Experience Music Beyond Imagination with AI Music Generator!

Music has always been a captivating art form that resonates with people across cultures. In recent years, advancements in artificial intelligence and deep learning have opened up new possibilities for generating music using neural networks. In this blog, we will explore the fascinating process of generating music using LSTM (Long Short-Term Memory) neural networks. Our focus will be on leveraging the MusicNet dataset, a comprehensive collection of classical music MIDI files. Additionally, we will discuss the instrumental role played by Intel DevCloud in facilitating efficient code execution, enabling faster training, and leveraging the power of Intel hardware resources.

To begin our musical journey, we need to import several essential libraries. These include pandas and numpy for data manipulation, mido for handling MIDI files, IPython for audio playback, and matplotlib and librosa for visualizing audio data. Furthermore, we rely on Keras, a popular deep learning framework, to construct and train our LSTM model. These libraries equip us with the necessary tools to process MIDI files, extract musical information, and implement our neural network.

Before diving into the code, we start by loading the MusicNet dataset metadata using pandas. The metadata provides valuable information about the available MIDI files, such as composer, title, and duration. Familiarizing ourselves with the structure and content of the dataset allows us to navigate through the vast collection of MIDI files and select specific compositions for music generation.

To gain a deeper understanding of the code, we load a MIDI file using the mido library. MIDI files consist of multiple tracks that contain musical events and messages. By inspecting the MIDI tracks, we can glean insights into the composition's structure and different musical elements. This step allows us to identify the specific tracks we are interested in for our LSTM model.

In our case, we focus on the compositions of Ludwig van Beethoven. By iterating through the tracks, we separate and store the tracks associated with the right hand of the piano. These tracks contain the melodic elements of the composition and serve as suitable training data for our LSTM model. This selection process ensures that our model learns patterns from the melodies composed by Beethoven.

With the MIDI tracks in hand, we define functions that parse the notes, identify the key signature, and convert the MIDI data into a more usable format. These functions allow us to extract crucial information, such as the pitch of each note, its duration, and velocity. By organizing the notes into a structured format, we lay a solid foundation for training our LSTM model.

Next, we preprocess the extracted notes by creating input-output pairs. We slide a window of fixed length (referred to as "phrase_len") along the notes. If the window contains a variety of different notes, we consider it a valid phrase and add it to our training data. This step ensures that our LSTM model learns diverse musical patterns and avoids generating monotonous or repetitive music. By incorporating this validation mechanism, we encourage the model to produce more musically interesting compositions.

With our data prepared, we proceed to construct the LSTM model using Keras. The model consists of an LSTM layer with 512 units, which is capable of capturing temporal dependencies in the music. We add a dropout layer to prevent overfitting and a dense layer with three output units representing the predicted values for the next note's pitch, duration, and velocity. The model is compiled using the Adam optimizer and Mean Absolute Error (MAE) as the loss function. These choices optimize the model parameters to generate musically coherent and expressive compositions.

After training the model, we can explore the exciting process of generating music. We define a tune generator function that utilizes the learned patterns to generate new music. Starting from a randomly selected point in the training data, we predict the next note using the LSTM model and append it to the pattern. This iterative process continues to generate a sequence of notes, resulting in a unique musical composition. The predicted notes are then converted back to MIDI format and saved as a new MIDI file, ready for playback and further exploration.

In the context of running the code, Intel DevCloud plays a crucial role in accelerating the execution process. DevCloud provides access to a vast range of Intel hardware resources, including CPUs, GPUs, and FPGAs. Leveraging DevCloud's high-performance computing capabilities allows us to train our LSTM model faster and more efficiently. By harnessing the power of Intel architectures, we can significantly reduce training time and explore larger datasets or more complex models.

In conclusion, this blog has introduced the process of generating music using LSTM neural networks and the MusicNet dataset. We discussed the code step by step, from loading the dataset and MIDI files to training the LSTM model and generating new compositions. Furthermore, we highlighted the significance of Intel DevCloud in enabling efficient code execution by leveraging the power of Intel hardware resources. By embracing the potential of AI-generated music, we can uncover new possibilities for creativity and explore the fascinating intersection of music and artificial intelligence.

To get more detailed explanation visit our Project's GitHub page
https://github.com/santhoshpandiarajan/AI-Music-Generator.git
And please see our other projects too!
Santhosh:https://github.com/santhoshpandiarajan
Subash:https://github.com/SUBASHPALVEL