sound and input vector

4 visualizzazioni (ultimi 30 giorni)
Vojta Pulda
Vojta Pulda il 25 Ott 2012
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

Risposta accettata

Walter Roberson
Walter Roberson il 25 Ott 2012
tones = {A1, G1, Fsharp};
song2 = horzcat(tones{song});

Più risposte (1)

Vojta Pulda
Vojta Pulda il 26 Ott 2012
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

Categorie

Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by