sound and input vector

조회 수: 8 (최근 30일)
Vojta Pulda
Vojta Pulda 2012년 10월 25일
Hi, if someone would know the answer, or some help with this, it would be really nice.
My aim is to have sound function generate a sequence of tones. I got tones defined based on their frequencies and are defined like this:
A1=cos(2*pi*440.0*timer)
What I would like to do is to take an input vector of numbers e.g.
song=[1 3 4 8 1 5 6];
where the numbers represents a single tone, and to let it be reproduced by the sound function. But as I have defined tones as A1... G1... etc. I can't do it better than having a for cycle that decides using if cycles which tone is going to be played based on the number on particular place in the vector song.
this is working for me now:
for i=1:number_of_notes
if song(i)==1
sound(G1);
elseif song(i)==2
sound(A1);
end
end
But this solution plays it to slowly with delays between single tones.
song2=[G1 A1];
sound(song2);
This do it much better as the "music" is smoother, but I don't know how to substitute numbers in song vector to tone names to play it smoother.
My idea was to create a text file, where I could use the for cycle mentioned above to store data tone names based on the song(i) value, but reading this file brought me again just to numbers (ascii codes of course).
My second idea was to use an xls file, where I could store the input song vector values, make a script to generate proper output in form of tone names G1, A1 etc, but here I am stucked on reading cells. I used
[text]=xlsread('test.xls',1,'A2:F2')
But Matlab told me that there is nothing in text, even when I checked formating of cells to text.
text = []
Does anybody know, what could I try more? Or is it just nonsense idea and the sound function won't work this way anyway? Thank you Vojta

채택된 답변

Walter Roberson
Walter Roberson 2012년 10월 25일
tones = {A1, G1, Fsharp};
song2 = horzcat(tones{song});

추가 답변 (1개)

Vojta Pulda
Vojta Pulda 2012년 10월 26일
Yes! That's it, so simple...really great idea, thank you very very much. I am always wondering how powerful Matlab is...and how simple it may look like.
Thank you.
Vojta

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by