Creating and Exporting MIDI files from Matlab 1D vector

8 visualizzazioni (ultimi 30 giorni)
Dear friends,
I have a 1D vector made of zeros and ones, where ones represent triggers, and zeros represent the absence of an event. I want to convert this vector to a MIDI file, and export it so I can use it in Digital Audio Workspaces. The vector length is expressed as a function of sample rate, which I keep fixed at 44100 Hz. Thus, for a one-second vector, I will have 44100 numbers either zeros or ones.
Now, I want to assign every 'one' in the vector a fixed MIDI event, say, a MIDI note n.60, with duration 0.1 seconds, channel 1, and offset in time coincidentally with the position of the reference 'one' number in the binary vector. For example, let's say in the vector the 22050 number is '1', then the MIDI file will have a 0.1sec long note 60 positioned at 0.5 sec.
Do you have any idea of how to create and export this MIDI file? Unfortunately I find no support on this elsewhere. Thanks a lot!
M.

Risposte (1)

Moksh
Moksh il 8 Set 2023
Hi Mattia,
Based on the information provided, I understand that you would like to generate an array of MIDI messages by identifying the occurrences of '1' values in a 1D vector consisting of zeros and ones. Additionally, you intend to export this resulting array.
To generate MIDI signals, the 'midimsg' function in MATLAB can be utilized. For storing these messages, I recommend employing a cell array. This cell array should have dimensions matching the 1D vector and should hold audio signals in the locations where a '1' is present. Subsequently, you can proceed to save this cell array externally as a '.mat' file.
Here is an example code showing the above-mentioned steps
% Generating a random vector of 0's and 1's
length = 20;
randomIntegers = randi([0, 1], 1, length);
zeros_ones = logical(randomIntegers);
% Cell array containing for MIDI signals
MIDI_array = cell(size(zeros_ones));
% Specify the audio message parameters
channel = 1;
note = 60;
velocity = 64;
duration = 2;
timestamp = 1;
% Iterate over the 0's and 1's array and at all the 1 positions write a midimsg in the cell array
for i = 1 : length
if zeros_ones(i) == 1
% Generate the audio message using 'midimsg' function at the '1' position
MIDI_array{i} = midimsg('Note',channel,note,velocity,duration,timestamp);
end
end
% Save the cell array externally as a .mat file
save("MidiMessages.mat", "MIDI_array");
For more understanding about the used functions please refer to the following documents.
Hope this helps!

Categorie

Scopri di più su Simulation, Tuning, and Visualization in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by