Make the half note rest for two beats
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to make a code that would play Twinkle Twinkle Little Star with two functions, one that generates the note (function generatenote2) and the other envelope's the note (function envelope) (Remove the overlay sound of notes upon intially starting the code). Its almost perfect but the only problem is that everytime it plays a half-note beat instead of a quarter-note beat, the rest (duration rest) is a quarter-rest instead of a half-rest. Could anyone look at this code and fix the issue I have. Here is the code and the functions.
function note = generatenote2(frequency,dur,samplingrate)
% frequency in Hz
% duration in count. 1 count = 4000 samples
% samplingrate = sampling frequency
count = 4000; % samples
samples = 1/samplingrate:1/samplingrate:dur*count/samplingrate;
note = sin(2*pi*frequency*samples)*1; %Multiply this by 2 or higher to increase the sound.
%Multiply note by 1 to play notes not too loud or too soft (mezzo-forte).
end
l
function adsr = envelope(dur)
% duration in count. 1 count = 4000 samples
% samplingrate = sampling frequency
% all elements are in percentage of duration
count = 4000; % # samples
sustainIntensity = 0.8; %0.8 % sustained level of sound
attackDur = 0.1*dur*count; % 1/10 of note duration in count
decayDur = 0.1*dur*count;
sustainDur = 0.6*dur*count;
releaseDur = 0.2*dur*count;
A=linspace(0,1,attackDur);
D=linspace(1,sustainIntensity,decayDur);
S=sustainIntensity*ones(1,sustainDur);
R=linspace(sustainIntensity,0,releaseDur);
adsr=horzcat(A,D,S,R);
l
%Code:
clear all;
close all;
clc;
% Author: Michael Kent
% generate_music.m
% dur = duration
fs = 16000; % sampling frequency (fs) %Set the tempo
count = 4000; % duration of one count in units of samples % How fast each note is played
%1 beat = 4000 count
%2 beats per measure = 2 * 4000 = 8000 count
Ts = 1/fs; % sampling time
Tt = 1; % total time
t = 0:Ts:Tt;
%This will play d. Twinkle, Twinkle, Little Star
%Has 2 beats per measure and each note is a quarter note.
% Playthe notes in the following order:
% [C5, C5, G5, G5, A5, A5, G5, G5, F5, F5, E5, E5, D5, D5, C5, ...
% G5, G5, F5, F5, E5, E5, D5, G5, G5, F5, F5, E5, E5, D5, ...
% C5, C5, G5, G5, A5, A5, G5, G5, F5, F5, E5, E5, D5, D5, C5]
dur = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 ...
1 1 1 1 1 1 2 1 1 1 1 1 1 2 ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2]; % note durations
freq = [523.25 523.25 783.99 783.99 880 880 783.99 783.99 ...
698.46 698.46 659.26 659.26 587.33 587.33 523.25 ...
783.99 783.99 698.46 698.46 659.26 659.26 587.33 ...
783.99 783.99 698.46 698.46 659.26 659.26 587.33 ...
523.25 523.25 783.99 783.99 880 880 783.99 783.99 ...
698.46 698.46 659.26 659.26 587.33 587.33 523.25]; % note frequencies
n = length(freq);
dr = 1; %duration rest = 1 count (quarter-note)
rest = zeros(1,count*dr);
%Here is where I attempt to make a rest for half-note
drest = 2; %duration rest = 2 count (half-note)
rest2 = zeros(1,count*dr);
song = []; % define an empty container
for k = 1:n
if dur(k) == 1
thisnote=generatenote2(freq(k),dur(k),fs).*envelope(dur(k));
song=horzcat(song,thisnote,rest);
else
thisnote=generatenote2(freq(k),dur(k),fs).*envelope(dur(k)); %Attempt to make half-rest with half-note
song=horzcat(song,thisnote,rest2);
end
end
sound(song,fs);
0 Kommentare
Akzeptierte Antwort
Voss
am 18 Feb. 2023
Looks like this:
rest2 = zeros(1,count*dr);
should be this:
rest2 = zeros(1,count*drest);
Otherwise, rest2 is the same as rest, and drest is not used.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Audio and Video Data finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!