Reputation: 2087
I would like to be able to take two vectors as inputs to a function in MATLAB such that the first has all of the frequencies I would like to play, and the second has the corresponding duration of each note. What I have so far is:
%% Initialize
clear; format short e;
%% Possible Notes
octave = [55 110 220 440 880];
for k = 1:length(octave)
pause = 0;
A(k) = octave(k);
As(k) = octave(k).*2^(1/12);
B(k) = octave(k).*2^(2/12);
C(k) = octave(k).*2^(3/12);
Cs(k) = octave(k).*2^(4/12);
D(k) = octave(k).*2^(5/12);
Ds(k) = octave(k).*2^(6/12);
E(k) = octave(k).*2^(7/12);
F(k) = octave(k).*2^(8/12);
Fs(k) = octave(k).*2^(9/12);
G(k) = octave(k).*2^(10/12);
Gs(k) = octave(k).*2^(11/12);
end
%% Notes and Durations
Notes = [D(2) D(3) D(2) D(2) C(3) D(2) D(2) A(3) A(2) ...
A(2) G(2) A(2) A(2) F(3) Fs(3) D(2)];
Times = [1/4 1/4 1/5 1/5 1/4 1/5 1/5 1/4 1/5 ...
1/5 1/4 1/5 1/5 1/4 1/8 1/4];
%% Play the Song
playSong(Notes, Times);
I have in my 'playSong' function the following:
function [song] = playSong(freqs, times)
fs = 44600;
makeNote = @(freq, time) cos(2*pi*[1:time]/fs * freq);
song = []
%% For Loop
for k = 1:length(freqs)
% make the song somehow
end
sound(song, fs)
end
I've been grinding over how to create my vector of sounds to be played, but can't seem to figure out how to do it. Does anyone know how I might go about this?
Upvotes: 2
Views: 2712
Reputation: 26069
(a) You can use sound
or soundsc
to convert your vector\matrix of signal data to sound.
(b) If you want to generate audio using the sound card and you have a 32-bit version of MATLAB and Data Acquisition Toolbox, see here an example how to use it.
(c) you can record audio to an audio object using audiorecorder
, and then play it using 'audioplayer'
You have several examples in all of these options on how to implement and play your notes.
Upvotes: 4