How to use MATLAB audio processing - make a piano - arrange and play music

Vignette - MATLAB Code Formatting

MBeautifier is an open source one-click beautifying m-code formatter based on MATLAB source code. It can be used directly in the MATLAB editor, and can be configured according to user habits. The minimum version of Matlab supported by MBeautifier is R2013b.

insert image description here

audioread, sound functions

filename: the location of the file plus the name, commonly used files on the desktop, the audio format is WAV, note that the file name is enclosed in quotation marks.

Special attention is that if the file is under the current file path under the MATLAB menu bar, you can only write the file name. If you do not call it in this path, you need to write in the complete address. The procedure is as follows:

[y,Fs]=audioread('C:\Users\Administrator\Desktop\1.wav');

Fs: Sampling frequency, the number of points taken for a continuous audio signal within 1 second.

y: The output audio digital signal, the default is n rows and 2 columns, where 2 means that the audio signal has two channels, and the value of n is equal to the duration of the audio signal multiplied by the sampling frequency Fs.

Piano Keys - Details

Find a group of two black keys and three white keys, the first white key from the left is c, the white key in the middle of the keyboard in the C key is 1, and the white keys behind it are 234567.
insert image description here
A Major for Piano
insert image description here

case test

m can pass an array to pronounce chords

function tunes2pianosound(m, time)

% m : a vector with tunes from pianonotes 1 to 88 played at the same time.
% time : change this for longer or shorter pianosound (rhythm)

len = length(m);
max_len = 0;

for i = 1:len
    note = ['all_notes\', num2str(m(i)), '.wav';]

    [y{
    
    i}, Fs] = audioread(note);
    plot(y{
    
    i})
    y{
    
    i} = y{
    
    i}(1:round(length(y{
    
    i})/time), :);

    if length(y{
    
    i}) > max_len
        max_len = length(y{
    
    i});
    end

end
y_sound = zeros(max_len, 2);
for i = 1:len
    y_sound = y_sound + y{
    
    i};
end
soundsc(y_sound, Fs)

1. size Get the number of rows and columns of the array

2. length The length of the array, that is, the larger value of the number of rows and the number of columns, which is equivalent to max(size(a))

Guess you like

Origin blog.csdn.net/qq_47452807/article/details/125823548