problem with solving overlapping sounds
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Geoff Hayes
2014-12-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!