You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Matlab file doesn't produce audio
3 views (last 30 days)
Show older comments
Hi, I am trying to produce an audio file out of the following matlab code. When I go to play these Matlab Files Matlab just hangs and I am unable to get any output out of them. Is there a specific way that these files have to be played to get a successful output? Any help is appreciated Matlab code below
I am trying to play the bach_fugue.mat file with the 2 matlab files below.
First File
clear;
load bach_fugue.mat;
fs = 11025; % set sampling frequency
bpm = 120; % set tempo in beats per minute
bps = bpm/60; % tempo in beats per second
spb = 1/bps; % seconds per beat (inverse of bps)
spp = spb/4; % seconds per pulse
sampp = fs*spp; % samples per pulse
voices = length(theVoices);
xx = zeros(1,560000);
for i = 1:voices % for loop that runs through different voices in theVoices
notenum = length(theVoices(i).noteNumbers);
n1 = 1;
% establish value for rest
rest = key2note(0,theVoices(i).startPulses(1)*spp,i);
n2 = n1 + length(rest) - 1;
xx(n1:n2) = xx(n1:n2) + rest;
n1 = n2 + 1;
% for loop that runs through each note in current voice
for k = 1:notenum
dur = theVoices(i).durations(k)*spp;
keynum = theVoices(i).noteNumbers(k);
firstPulse = theVoices(i).startPulses(k);
tone = key2note(keynum, dur, i);
n2 = n1 + length(tone) - 1;
xx(n1:n2) = xx(n1:n2) + tone;
n1 = n2 + 1;
% conditional statement to create inter-note pauses
if k < notenum
if (theVoices(i).startPulses(k+1)-firstPulse) > dur
pause = key2note(0,theVoices(i).startPulses(k+1)-(firstPulse)*spp,i);
n2 = n1 + length(pause) - 1;
xx(n1:n2) = xx(n1:n2) + pause;
n1 = n2 + 1;
end
end
end
end
Index exceeds the number of array elements. Index must not exceed 560000.
soundsc(xx, fs);
specgram(xx,512,fs);
Second File
function xx = key2note(keynum, dur, voicenum)
fs = 11025;
tt = 0:(1/fs):dur;
if (keynum == 0)
xx = zeros(1, length(tt)); % pause if no note
else
freq = 440*2^((keynum-49)/12);
if (voicenum == 1)
f = freq*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
elseif (voicenum == 2)
f = freq*[1 3 5]';
A = 0.8*[1 1/2 1/4];
elseif (voicenum == 3)
f = 1.5*freq*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
end
xx = A*cos(2*pi*f*tt);
% ADSR Envelope
if( dur>0.15 )
E = interp1([0,0.05,0.1,dur-0.05,dur],[0,1,0.9,0.8,0],tt);
elseif( dur>0.10 )
E = interp1([0,0.05,dur-0.05,dur],[0,1,0.8,0],tt);
else
E = interp1([0,0.05,dur],[0,1,0],tt);
end
% multiply xx by ADSR envelope
xx = xx.*E;
end
end
10 Comments
Walter Roberson
on 7 Mar 2024
We need bach_fugue.mat to test with.
Voss
on 7 Mar 2024
xx = zeros(1,560000);
This makes xx length 560000. No subsequent part of the code increases the length of xx, only adds new notes onto existing consecutive elements of xx. So whatever length xx is initialized to has to be enough to store all the notes, and apparently 560000 is not enough samples to store all the notes.
Walter Roberson
on 7 Mar 2024
Your song exceeds 1e8 in size.
load bach_fugue.mat;
fs = 11025; % set sampling frequency
bpm = 120; % set tempo in beats per minute
bps = bpm/60; % tempo in beats per second
spb = 1/bps; % seconds per beat (inverse of bps)
spp = spb/4; % seconds per pulse
sampp = fs*spp; % samples per pulse
voices = length(theVoices);
xx = zeros(1,1e8);
for i = 1:voices % for loop that runs through different voices in theVoices
notenum = length(theVoices(i).noteNumbers);
n1 = 1;
% establish value for rest
rest = key2note(0,theVoices(i).startPulses(1)*spp,i);
n2 = n1 + length(rest) - 1;
xx(n1:n2) = xx(n1:n2) + rest;
n1 = n2 + 1;
% for loop that runs through each note in current voice
for k = 1:notenum
dur = theVoices(i).durations(k)*spp;
keynum = theVoices(i).noteNumbers(k);
firstPulse = theVoices(i).startPulses(k);
tone = key2note(keynum, dur, i);
n2 = n1 + length(tone) - 1;
disp(-[n1, n2]), whos xx
xx(n1:n2) = xx(n1:n2) + tone;
n1 = n2 + 1;
% conditional statement to create inter-note pauses
if k < notenum
if (theVoices(i).startPulses(k+1)-firstPulse) > dur
pause = key2note(0,theVoices(i).startPulses(k+1)-(firstPulse)*spp,i);
n2 = n1 + length(pause) - 1;
disp([n1, n2]), whos xx
xx(n1:n2) = xx(n1:n2) + pause;
n1 = n2 + 1;
end
end
end
end
-4136 -5514
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
5515 45480
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-45481 -46859
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
46860 96472
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-96473 -98126
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
98127 168411
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-168412 -170065
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
170066 259644
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-259645 -261298
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
261299 370170
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-370171 -371549
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
371550 488690
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-488691 -490069
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
490070 616857
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-616858 -618511
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
618512 765971
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-765972 -767625
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
767626 934379
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-934380 -936033
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
936034 1122080
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-1122081 -1123459
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
1123460 1317775
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-1317776 -1319154
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
1319155 1523117
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-1523118 -1524771
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
1524772 1749406
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-1749407 -1751060
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
1751061 1994989
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-1994990 -1996368
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
1996369 2248565
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-2248566 -2249944
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
2249945 2511788
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-2511789 -2517301
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
2517302 2821867
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-2821868 -2823246
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
2823247 3133325
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-3133326 -3134704
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
3134705 3454430
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-3454431 -3455533
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
3455534 3784905
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-3784906 -3786284
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
3786285 4125303
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-4125304 -4126682
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
4126683 4475348
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-4475349 -4476727
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
4476728 4835040
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-4835041 -4836419
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
4836420 5204379
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-5204380 -5205758
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
5205759 5583365
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-5583366 -5584744
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
5584745 5971998
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-5971999 -5973377
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
5973378 6370278
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-6370279 -6371932
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
6371933 6789504
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-6789505 -6792261
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
6792262 7229127
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-7229128 -7230781
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
7230782 7686941
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-7686942 -7688595
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
7688596 8164049
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-8164050 -8165703
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
8165704 8660450
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-8660451 -8663207
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
8663208 9177248
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-9177249 -9178902
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
9178903 9712237
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-9712238 -9713891
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
9713892 10266520
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-10266521 -10269277
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
10269278 10841199
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-10841200 -10843956
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
10843957 11435172
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-11435173 -11437929
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
11437930 12048439
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-12048440 -12051196
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
12051197 12681000
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-12681001 -12686513
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
12686514 13368685
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-13368686 -13370064
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
13370065 14056371
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-14056372 -14057750
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
14057751 14753704
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-14753705 -14755083
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
14755084 15460684
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-15460685 -15462063
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
15462064 16177310
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-16177311 -16178689
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
16178690 16903583
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-16903584 -16907718
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
16907719 17664309
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-17664310 -17665688
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
17665689 18429170
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-18429171 -18430549
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
18430550 19203678
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-19203679 -19205057
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
19205058 19987833
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-19987834 -19989212
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
19989213 20781634
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-20781635 -20783013
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
20783014 21585082
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-21585083 -21589217
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
21589218 22422983
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-22422984 -22424362
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
22424363 23265019
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-23265020 -23266398
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
23266399 24116702
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-24116703 -24118081
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
24118082 24978032
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-24978033 -24979411
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
24979412 25849008
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-25849009 -25850387
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
25850388 26729631
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-26729632 -26731010
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
26731011 27619901
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-27619902 -27621280
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
27621281 28519818
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-28519819 -28521472
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
28521473 29440682
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-29440683 -29442061
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
29442062 30369540
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-30369541 -30370919
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
30370920 31308045
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-31308046 -31313558
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
31313559 32359555
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-32359556 -32362312
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
32362313 33416578
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-33416579 -33418232
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
33418233 34491792
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-34491793 -34493446
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
34493447 35608350
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-35608351 -35611107
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
35611108 36742548
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-36742549 -36744202
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
36744203 37894937
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-37894938 -37896591
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
37896592 39066620
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-39066621 -39068274
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
39068275 40257596
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-40257597 -40258975
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
40258976 41456566
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-41456567 -41457945
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
41457946 42665183
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-42665184 -42666837
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
42666838 43894747
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-43894748 -43896401
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
43896402 45143605
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-45143606 -45149118
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
45149119 46459715
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-46459716 -46461369
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
46461370 47785748
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-47785749 -47787402
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
47787403 49131074
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-49131075 -49132453
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
49132454 50484394
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-50484395 -50485773
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
50485774 51847361
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-51847362 -51849015
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
51849016 53231275
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-53231276 -53232929
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
53232930 54634483
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-54634484 -54639996
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
54639997 56104943
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-56104944 -56106597
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
56106598 57585326
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-57585327 -57586980
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
57586981 59085002
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-59085003 -59086381
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
59086382 60592672
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-60592673 -60594051
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
60594052 62109989
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-62109990 -62111643
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
62111644 63648253
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-63648254 -63649907
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
63649908 65205811
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-65205812 -65211324
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
65211325 66830621
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-66830622 -66832275
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
66832276 68465354
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-68465355 -68467008
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
68467009 70119380
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-70119381 -70122137
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
70122138 71793803
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-71793804 -71795457
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
71795458 73486417
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-73486418 -73488071
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
73488072 75220375
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-75220376 -75223132
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
75223133 76971973
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-76971974 -76973627
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
76973628 78741762
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-78741763 -78743416
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
78743417 80552895
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-80552896 -80554274
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
80554275 82369265
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-82369266 -82370644
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
82370645 84195282
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-84195283 -84196936
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
84196937 86042246
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-86042247 -86045003
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
86045004 87909607
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-87909608 -87911261
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
87911262 89795158
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-89795159 -89797915
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
89797916 91701106
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-91701107 -91702760
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
91702761 93625245
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-93625246 -93626899
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
93626900 95568678
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-95568679 -95570332
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
95570333 97531404
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-97531405 -97534161
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
97534162 99514527
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
-99514528 -99516181
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
99516182 101515841
Name Size Bytes Class Attributes
xx 1x100000000 800000000 double
Index exceeds the number of array elements. Index must not exceed 100000000.
whos xx
soundsc(xx, fs);
specgram(xx,512,fs);
function xx = key2note(keynum, dur, voicenum)
fs = 11025;
tt = 0:(1/fs):dur;
if (keynum == 0)
xx = zeros(1, length(tt)); % pause if no note
else
freq = 440*2^((keynum-49)/12);
if (voicenum == 1)
f = freq*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
elseif (voicenum == 2)
f = freq*[1 3 5]';
A = 0.8*[1 1/2 1/4];
elseif (voicenum == 3)
f = 1.5*freq*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
end
xx = A*cos(2*pi*f*tt);
% ADSR Envelope
if( dur>0.15 )
E = interp1([0,0.05,0.1,dur-0.05,dur],[0,1,0.9,0.8,0],tt);
elseif( dur>0.10 )
E = interp1([0,0.05,dur-0.05,dur],[0,1,0.8,0],tt);
else
E = interp1([0,0.05,dur],[0,1,0],tt);
end
% multiply xx by ADSR envelope
xx = xx.*E;
end
end
Walter Roberson
on 7 Mar 2024
This is why it does not play anything: it reaches the end of array xx
Walter Roberson
on 7 Mar 2024
You can set a buffer size and quit filling it when you get to the end of the buffer.
load bach_fugue.mat;
fs = 11025; % set sampling frequency
bpm = 120; % set tempo in beats per minute
bps = bpm/60; % tempo in beats per second
spb = 1/bps; % seconds per beat (inverse of bps)
spp = spb/4; % seconds per pulse
sampp = fs*spp; % samples per pulse
voices = length(theVoices);
maxXX = 560000;
xx = zeros(1,maxXX);
for i = 1:voices % for loop that runs through different voices in theVoices
notenum = length(theVoices(i).noteNumbers);
n1 = 1;
% establish value for rest
rest = key2note(0,theVoices(i).startPulses(1)*spp,i);
n2 = n1 + length(rest) - 1;
xx(n1:n2) = xx(n1:n2) + rest;
n1 = n2 + 1;
% for loop that runs through each note in current voice
for k = 1:notenum
dur = theVoices(i).durations(k)*spp;
keynum = theVoices(i).noteNumbers(k);
firstPulse = theVoices(i).startPulses(k);
tone = key2note(keynum, dur, i);
n2 = n1 + length(tone) - 1;
if n2 > maxXX
margin = maxXX - n1 + 1;
tone = tone(1:margin);
n2 = maxXX;
end
disp(-[n1, n2])
xx(n1:n2) = xx(n1:n2) + tone;
n1 = n2 + 1;
if n1 > maxXX; break; end
% conditional statement to create inter-note pauses
if k < notenum
if (theVoices(i).startPulses(k+1)-firstPulse) > dur
pause = key2note(0,theVoices(i).startPulses(k+1)-(firstPulse)*spp,i);
n2 = n1 + length(pause) - 1;
if n2 > maxXX
margin = maxXX - n1 + 1;
pause = pause(1:margin);
n2 = maxXX;
end
disp([n1, n2])
xx(n1:n2) = xx(n1:n2) + pause;
n1 = n2 + 1;
if n1 > maxXX; break; end
end
end
end
end
-4136 -5514
5515 45480
-45481 -46859
46860 96472
-96473 -98126
98127 168411
-168412 -170065
170066 259644
-259645 -261298
261299 370170
-370171 -371549
371550 488690
-488691 -490069
490070 560000
-48236 -49614
49615 398280
-398281 -399659
399660 560000
-136436 -137814
137815 560000
soundsc(xx, fs);
Timeout occurred while trying to communicate to the device.
specgram(xx,512,fs);
function xx = key2note(keynum, dur, voicenum)
fs = 11025;
tt = 0:(1/fs):dur;
if (keynum == 0)
xx = zeros(1, length(tt)); % pause if no note
else
freq = 440*2^((keynum-49)/12);
if (voicenum == 1)
f = freq*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
elseif (voicenum == 2)
f = freq*[1 3 5]';
A = 0.8*[1 1/2 1/4];
elseif (voicenum == 3)
f = 1.5*freq*[1 2 3 4]';
A = [1 1/2 1/4 1/8];
end
xx = A*cos(2*pi*f*tt);
% ADSR Envelope
if( dur>0.15 )
E = interp1([0,0.05,0.1,dur-0.05,dur],[0,1,0.9,0.8,0],tt);
elseif( dur>0.10 )
E = interp1([0,0.05,dur-0.05,dur],[0,1,0.8,0],tt);
else
E = interp1([0,0.05,dur],[0,1,0],tt);
end
% multiply xx by ADSR envelope
xx = xx.*E;
end
end
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)