Azzera filtri
Azzera filtri

I need to create a harmoniser in matlab (audio harmonisation)

2 visualizzazioni (ultimi 30 giorni)
I’m looking for some codes related to performing Harmonisation in matlab.

Risposte (1)

albara
albara il 11 Mag 2023
Harmonization in the context of music typically refers to creating a chord progression, or harmonies, to accompany a melody. There isn't a standard Matlab function for music harmonization, but you could design a function that assigns chords to each note of the melody based on a set of rules. Here's a basic example where we assume the melody is a sequence of MIDI note numbers, and we harmonize in the key of C major.
function [chords] = harmonize(melody)
% Define the chords in the key of C major.
% Each chord is represented as a vector of MIDI note numbers.
C = [60, 64, 67]; % C major
Dm = [62, 65, 69]; % D minor
Em = [64, 67, 71]; % E minor
F = [65, 69, 72]; % F major
G = [67, 71, 74]; % G major
Am = [69, 72, 76]; % A minor
Bdim = [71, 74, 77]; % B diminished
chords = {};
for i = 1:length(melody)
note = melody(i);
switch note
case {60, 72} % C
chord = C;
case {62, 74} % D
chord = Dm;
case {64, 76} % E
chord = Em;
case {65, 77} % F
chord = F;
case {67, 79} % G
chord = G;
case {69, 81} % A
chord = Am;
case {71, 83} % B
chord = Bdim;
otherwise
error('Note %d is not in the key of C major.', note);
end
chords{i} = chord;
end
end
You can use this function by passing a melody as an argument. For example:
melody = [60, 62, 64, 65, 67, 69, 71, 72];
chords = harmonize(melody);
This will return chords, a cell array where each cell is a chord (represented as a vector of MIDI note numbers) that harmonizes with the corresponding note in the melody.
Remember, this is a very basic harmonization algorithm. Real music often contains more complex harmonies, and the choice of chords can depend on factors such as the melody's rhythm and the overall structure of the song.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer
Give me your feedback
Thanks
  3 Commenti
albara
albara il 11 Mag 2023
Harmonizing an audio signal is more complex because you first need to extract the melody, or sequence of notes, from the audio. This involves techniques such as pitch detection and note segmentation.
Once you have the melody, you can harmonize it using the method described in the previous response. Then, you would generate audio for the harmony and mix it with the original audio.
Here is a simple example of how you might approach this in Matlab. Note that this code requires the Audio Toolbox for reading and writing audio files, and the Signal Processing Toolbox for the pitch detection.
% Read the audio file.
[audioIn, fs] = audioread('melody.wav');
% Convert to mono if necessary.
if size(audioIn, 2) == 2
audioIn = mean(audioIn, 2);
end
% Extract the melody using pitch detection.
pitch = pitch(audioIn, fs);
% Convert pitches to MIDI note numbers.
midiNotes = 69 + 12 * log2(pitch / 440);
% Harmonize the melody.
chords = harmonize(midiNotes);
% Generate audio for the harmony.
harmony = zeros(size(audioIn));
for i = 1:length(chords)
if ~isempty(chords{i})
% Generate a chord by adding sine waves for each note.
for note = chords{i}
freq = 440 * 2^((note - 69) / 12);
harmony = harmony + sin(2 * pi * freq * (0:length(audioIn)-1)' / fs);
end
end
end
% Normalize the harmony to avoid clipping.
harmony = harmony / max(abs(harmony));
% Mix the harmony with the original audio.
mixedAudio = audioIn + harmony;
% Write the result to a new audio file.
audiowrite('harmonized.wav', mixedAudio, fs);
This code first reads an audio file and extracts the melody using the pitch function. It then harmonizes the melody using the harmonize function defined in the previous response. Next, it generates audio for the harmony by adding together sine waves for each note in the chord. Finally, it mixes the harmony with the original audio and writes the result to a new audio file.
This is a very basic example and may not give satisfactory results for complex music. A real-world application would likely require more advanced techniques for pitch detection, note segmentation, and harmony generation.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer
Anoushka
Anoushka il 13 Mag 2023
can you share a sample audio i can use to run this code? because the signals I tried are not working with the function

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by