mercredi 28 janvier 2015

Matlab Psychtoolbox: left to right moving sound using openAL

I'm using matlab psychtoolbox with openAL to make a pink noise burst moving slowly from left to right in the virtual space. The sound should move at ~1 meter in front of the listener, in a straight line. I tried to modify the openAL demo I found (i.e. AudioTunnel3D). I managed to have the sound moving from left to right by updating the position on the x axis with a for loop. Though, I guess there is a better way, e.g. by using all the functions (AL.VELOCITY; AL.DIRECTION) that openAL provide. Plus the result I got is not satisfacting: the sound get closer to the left hear and then kind of jump on the other side and then slowly fade away. Though, right now I'm a bit stuck, can you help me move one step forward maybe giving me a hint about how to use the velocity and direction parameters properly. This is my ugly code, THANKS A LOT IN ADVANCE FOR YOU HELP! (a good tutorial for dummies would also be good)



nsources = 1;

% Establish key mapping: ESCape aborts, Space toggles between auto-
% movement of sound source or user mouse controlled movement:
KbName('UnifyKeynames');
space = KbName('space');
esc = KbName('ESCAPE');

% Initialize OpenAL subsystem at debuglevel 2 with the default output device:
InitializeMatlabOpenAL(2);

% Generate one sound buffer:
buffers = alGenBuffers(nsources);

% Query for errors:
alGetString(alGetError)

% Try to load some impressive sound...
sounddir = [PsychtoolboxRoot 'PsychDemos/SoundFiles/'];
soundfiles = dir([sounddir '*.wav']);

alListenerfv(AL.POSITION, [0, 0, 0]);
alListenerfv(AL.VELOCITY, [0, 0, 0]);

if IsOSX
alcASASetListener(ALC.ASA_REVERB_ON, 1);
alcASASetListener(ALC.ASA_REVERB_QUALITY, ALC.ASA_REVERB_QUALITY_Max);
alcASASetListener(ALC.ASA_REVERB_ROOM_TYPE, ALC.ASA_REVERB_ROOM_TYPE_Cathedral);
end

% Create a sound source:
sources = alGenSources(nsources);

perm = randperm(nsources);

%Assign soundname
soundname = [sounddir 'motor_a8.wav'];

% Load it...
[mynoise freq]= wavread(soundname);
mynoise = mynoise(:, 1);

% Convert it...
mynoise = int16(mynoise * 32767);

% Fill our sound buffer with the data from the sound vector. Tell AL that its
% a 16 bpc, mono format, with length(mynoise)*2 bytes total, to be played at
% a sampling rate of freq Hz. The AL will resample this to the native device
% sampling rate and format at buffer load time.
alBufferData( buffers, AL.FORMAT_MONO16, mynoise, length(mynoise)*2 , freq);

% Attach our buffer to it: The source will play the buffers sound data.
%alSourceQueueBuffers(sources(i), 1, buffers(i));

alSourceQueueBuffers(sources, 1, buffers);
%alSourcei(sources, AL.BUFFER, buffers);


% Switch source to looping playback: It will repeat playing the buffer until
% its stopped.
alSourcei(sources, AL.LOOPING, AL.TRUE);

% Set emission volume to 100%, aka a gain of 1.0:
alSourcef(sources, AL.GAIN, 1);

% alSourcef(sources(i), AL.CONE_INNER_ANGLE, 30);
% alSourcef(sources(i), AL.CONE_OUTER_ANGLE, 270);

pos= -5;

alSource3f(sources, AL.POSITION, pos, 0, -5);
alSource3f(sources, AL.DIRECTION, 1, 0, 0);
alSource3f(sources, AL.VELOCITY, 0, 0, 0); %usato per simulare un effetto doppler


if IsOSX
% Source emits some sound that gets reverbrated in room:
alcASASetSource(ALC.ASA_REVERB_SEND_LEVEL, sources, 0.0);
end


% Start playback for these sources:
alSourcePlay(sources);




while 1
% Check keyboard:
[isdown dummy, keycode]=KbCheck;
if isdown
if keycode(esc)
break;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%HERE IS THE LOOP TO MAKE THE SOUND MOVING%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:nsources
if pos < 5
pos = pos+0.001;
x = pos;
y = 0;
z = 0;
end
alSource3f(sources, AL.POSITION, x, y, z);


end

end


% Wait a bit:
WaitSecs(0.1);

% Delete sources:
alDeleteSources(nsources, sources);


% Wait a bit:
WaitSecs(0.1);

% Delete buffer:
alDeleteBuffers(nsources, buffers);

% Wait a bit:
WaitSecs(0.1);


% Shutdown OpenAL:
CloseOpenAL;

% Done. Bye.
return;

Aucun commentaire:

Enregistrer un commentaire