How would I get the part to get the song to play backwards working correctly? How would I add a fade to the end of the song? and how would I get the program to ask all these questions and then play one song instead of one for each question? THANKS!!

조회 수: 15 (최근 30일)
%%Constants and inputs
setFreq = 440;
setBarLength = 1;
setRate = 8192;
%%Jukebox Code
%%%Opening menu. Give list of options, and ask user to make a selection.
fprintf(' 1.)Super Mario Bros \n 2.)Music for Prague \n 3.)Never Gonna Give You Up \n 4.)Siren \n 5.)Passacaglia \n 6.)Let It Go \n 7.)Go T \n 8.)Dies Irae \n 9.)Random \n')
random = rand;
%%%Saves the user-defined song into the variable 'song' by calling the
%%%proper function.
switch input ('Pick a song!(#1-9)-->')
case 1
song = SuperMarioBros(setFreq,setBarLength,setRate);
case 2
song = MusicForPrague(setFreq,setBarLength,setRate);
case 3
song = NeverGonna(setFreq,setBarLength,setRate);
case 4
song = Siren(setFreq,setBarLength,setRate);
case 5
song = Passacaglia(setFreq,setBarLength,setRate);
case 6
song = LetItGo(setFreq,setBarLength,setRate);
case 7
song = GoT(setFreq,setBarLength,setRate);
case 8
song = DiesIrae(setFreq,setBarLength,setRate);
case 9
if (random <= .125)
song = SuperMarioBros(setFreq,setBarLength,setRate);
elseif (random > .125) && (random <= .25)
song = MusicForPrague(setFreq,setBarLength,setRate);
elseif (random > .25) && (random <= .375)
song = NeverGonna(setFreq,setBarLength,setRate);
elseif (random > .375) && (random <= .5)
song = Siren(setFreq,setBarLength,setRate);
elseif (random > .5) && (random <= .625)
song = Passacaglia(setFreq,setBarLength,setRate);
elseif (random > .625) && (random <= .75)
song = LetItGo(setFreq,setBarLength,setRate);
elseif (random > .75) && (random <= .875)
song = GoT(setFreq,setBarLength,setRate);
elseif (random > .875) && (random <= 1)
song = DiesIrae(setFreq,setBarLength,setRate);
end
otherwise
input('Try Again -->');
end
%%%Asks user how much to change volume, and then change it.
switch input('How loud would you like it?(#1-10)-->')
case 1
sound(song);
case 2
sound(2.*song);
case 3
sound(3.*song);
case 4
sound(4.*song);
case 5
sound(5.*song);
case 6
sound(6.*song);
case 7
sound(7.*song);
case 8
sound(8.*song);
case 9
sound(9.*song);
case 10
sound(10.*song);
otherwise
input('Try Again-->')
end
%%%Asks user whether they want to play the song backwards, and then do
%%%it
input('Do you want to play it backwards?(y/n)-->','s');
if 'y'
sound(fliplr(song));
else
sound(song);
end
%%%Asks whether user wants to add a fade over last two bars, and then
%%%do it
% input('Do you want to add a fade over the last two bars?(y/n)-->');
%%%Play the song using the function 'sound()'
%%%Wait until the song is completed before proceeding. Use the
%%%function 'pause(time)', where time is the longest dimension of the
%%%song array. Since the sampling frequency is 8192 per second, you
%%%should wait (dimension)/rate seconds.
%%%Goodbye message
%%%All of these songs are files on my computer, so they do run.

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 9월 25일
Brandon - delay calling sound (more on this later) until you have modified the song that you wish to play. For example, if changing the "volume", then instead of doing
case 2
sound(2.*song);
do instead
case 2
song = 2*song;
You would do something similar for the fade out and the reverse - make a change to the song array for each question posed to the user.
So once you have selected the song, changed the volume, added the fade, reversed it, then you can play it. I noticed in your code that you added a comment to use pause to ensure that the song plays before proceeding with the next step. Rather than doing this, try using the audioplayer instead with the playblocking method instead. This will do the same thing as adding a pause (control will be blocked/held until the song has played).
You ask how to play the song backwards correctly. If you just re-order the song using fliplr then this should work provided that song is a row vector. If song is a column vector, then calling fliplr as
song = fliplr(song);
will have no effect and so you will want to use flipud instead. Try the following
if iscolumn(song)
song = flipud(song);
else
song = fliplr(song);
end
Try the above and see what happens!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by