Using sound function on Mac? r2014a

6 views (last 30 days)
Doug
Doug on 31 Jul 2014
Commented: Geoff Hayes on 31 Jul 2014
_

Answers (1)

Geoff Hayes
Geoff Hayes on 31 Jul 2014
Doug - check out the documentation for sound, and in particular
sound(y) sends audio signal y to the speaker at the default sample rate of 8192 hertz.
sound(y,Fs) sends audio signal y to the speaker at sample rate Fs.
In the first example, where the sample rate is 8192 Hz, we must provide 8192 samples for every second's worth of data in our vector y. So if y corresponds to 5 seconds of sound, then we must supply 5*8192 samples. Likewise, in the second example, if there is T seconds of sound in y, we must supply T*Fs samples.
In your code, you have written
sound(note_freq(notes(i)),samp_rate)
and so are supplying one sample for the samp_rate (which is at least 80). Moreover, you are supplying a frequency, when what we should be doing instead is providing data that has been sampled at that frequency.
Your code is for the first dozen or so notes of Twinkle, Twinkle, Little Star. Each note is played at the frequency assigned to that note, and so we must come up with a number of samples for each note. To make things easy, let's assume that each note is one second long, and our sampling rate is 8192. We are going to use the sine wave to simulate the sound of the note for the given frequency, and build a vector of samples for each note:
fs = 8192;
notes = ['c' 'c' 'g' 'g' 'a' 'a' 'g' 'f' 'f' 'e' 'e' 'd' 'd' 'c'];
n = length(notes);
% pre-allocate memory to the sound vector
y = zeros(1,n*fs);
% we assume one second worth of data per note, so build a time vector over
% the one second interval of fs number of equally spaced points
t = linspace(0,1,fs);
for k=1:n
% get the frequency for this note using your function
frq = note_freq(notes(k));
% build the fs samples for this note and save to the sound
% vector
y(1,1+fs*(k-1):fs*k) = sin(2*pi*frq*t);
end
% now play the sound
sound(y,fs);
The above produces a sound that is recognizable as Twinkle, Twinkle, Little Star, but as you indicated in your code, there is a pause at the seventh note (and every seventh note thereafter). So we could modify the above to extend every seventh note to two seconds instead of the one
% pre-allocate memory to the sound vector
y = zeros(1,(n+2)*fs);
v = 1;
for k=1:n
% get the frequency for this note using your function
frq = note_freq(notes(k));
% build the fs samples for this note and save to the sound
% vector
if mod(k,7)==0
% extend this note by two seconds
y(1,1+fs*(v-1):fs*(v+1)) = sin(2*pi*frq*[t t+(1+1/fs)]);
v = v + 2;
else
y(1,1+fs*(v-1):fs*v) = sin(2*pi*frq*t);
v = v + 1;
end
end
Try it and see what happens!
  3 Comments
Doug
Doug on 31 Jul 2014
Oh got it! Thanks so much!
Geoff Hayes
Geoff Hayes on 31 Jul 2014
Doug - why did you remove your question?

Sign in to comment.

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!