problem with solving overlapping sounds
7 views (last 30 days)
Show older comments
I'm currently trying to give my matlab game sound. To stop the sounds from overlapping I found a solution and changed the data accordingly.
SOUND_FILE_NAMES = {'.\sounds\pacman_beginning.wav', '.\sounds\pacman_chomp.wav', ...
'.\sounds\pacman_death.wav', '.\sounds\pacman_eatfruit.wav', ...
'.\sounds\pacman_eatghost.wav', '.\sounds\pacman_extrapac.wav', ...
'.\sounds\pacman_intermission.wav', '.\sounds\pacman_moving.wav'};
SOUND_KEYS = {'Beginning', 'chomp', 'death', 'eatfruit', 'eatghost', ...
'extrapac', 'intermission', 'moving'};
SOUND_SAMPLES = [5,5,5,3,3,2,6,5];
obj.audioPlayers = struct();
for i = 1 : length( SOUND_FILE_NAMES )
[y, Fs] = audioread(SOUND_FILE_NAMES{i});
str = 'audioplayer(y,Fs)';
for j = 2 : SOUND_SAMPLES(i);
str = sprintf('%s, audioplayer(y,Fs)', str );
end
eval( sprintf('obj.audioPlayers.(SOUND_KEYS{i}) = {%s};', str ));
end
PlaySound(obj,'Beginning');
and the function
function PlaySound( obj, soundName )
done = false;
i = 1;
while ~done && i < length(obj.(soundName))
if ~obj.(soundName){i}.isplaying()
obj.(soundName){i}.play();
done = true;
end
i = i + 1;
end
end
But I only get the error
Reference to non-existent field 'Beginning'
Error in PlaySound (line 7)
while ~done && i < length(obj.(soundName))
I don't know how to solve this problem.
0 Comments
Accepted Answer
More Answers (1)
Geoff Hayes
on 18 Dec 2014
Gino - the error message is telling you that there is no field named Beginning in your structure obj which you try to access as *obj.('Beginning').
Note how you are creating the fields in your object:
eval( sprintf('obj.audioPlayers.(SOUND_KEYS{i}) = {%s};', str ));
so the field can most likely be found at
obj.audioplayers.('Beginning')
So using the above code instead will probably help, but I'm not all that clear on what you have intended to do with this object. In the code that initializes the structure,
for i = 1 : length( SOUND_FILE_NAMES )
[y, Fs] = audioread(SOUND_FILE_NAMES{i});
str = 'audioplayer(y,Fs)';
for j = 2 : SOUND_SAMPLES(i);
str = sprintf('%s, audioplayer(y,Fs)', str );
end
eval( sprintf('obj.audioPlayers.(SOUND_KEYS{i}) = {%s};', str ));
end
the inner for loop builds the string str but never makes use of the indexing variable j and so the same data is appended to the string in some sort of comma separated list. What is the intent behind this variable?
Also, try avoid using the eval as it makes debugging challenging. Think of an alternate for this line.
0 Comments
See Also
Categories
Find more on Audio and Video Data 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!