Experts, please help to solve: Index in position 2 exceeds array bounds. Index must not exceed 9.Error in (line 30) if drumTable(sound,beat) == 1
3 views (last 30 days)
Show older comments
% Define drum sounds
open_hihat = audioread('open_hihat.wav');
closed_hihat = audioread('closed_hihat.wav');
snare_drum = audioread('snare_drum.wav');
bass_drum = audioread('bass_drum.wav');
tom_toms = audioread('tom_toms.wav');
cymbals = audioread('cymbals_sound.wav');
% Define tempo and song length
fs=44100;
tempo = 130; % beats per minute
beatLength = 60/tempo; % length of one beat in seconds
numBeats = 457;
songLength = numBeats*1;
% Create drum machine table
drumTable = [0 1 1 1 0 0 0 1 1;
0 0 0 1 0 1 1 0 1;
0 1 0 1 1 0 0 1 1;
1 1 1 1 1 0 0 0 1;
0 0 0 1 0 1 1 0 1;
0 0 0 1 0 1 1 0 1];
% Create empty audio signal for the song
song = zeros(songLength*fs, 2);
% Generate the drum sounds for each beat
for beat = 1:numBeats
for sound = 1:size(drumTable,1)
if drumTable(sound,beat) == 1
switch sound
case 1
drumSound = open_hihat;
case 2
drumSound = closed_hihat;
case 3
drumSound = snare_drum;
case 4
drumSound = bass_drum;
case 5
drumSound = tom_toms;
case 6
drumSound = cymbals;
end
startIndex = round((beat-1)*beatLength*fs)+1;
endIndex = startIndex + size(drumSound,1) - 1;
song(startIndex:endIndex,:) = song(startIndex:endIndex,:) + drumSound;
end
end
end
% Normalize the song and save as a wave file
song = song/max(abs(song(:)));
audiowrite('drumMachineSong.wav', song, fs);
0 Comments
Answers (1)
Walter Roberson
on 7 Apr 2023
if drumTable(sound,beat) == 1
in order to have that test, then beat must not exceed the number of columns of drumTable. You initialize drumTable as 9 columns and you never expand it, so beat must be at most 9. But beat is potentially up to the number of beats which you assigned as a number greater than 400.
2 Comments
Walter Roberson
on 7 Apr 2023
I would suggest to you that if you were to document the meaning of the rows and columns in the beat table, that you would have a clearer understanding of what needed to be done.
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!