Plugins-soundmanager-example code

From Bcontrol
% The following code runs stand-alone (assuming newstartup.m has been run).
%
% 

% First, make sure that dispatcher exists and is up:
dispatcher('init');

% Now let's make an object that is of the @soundmanager class. Normally,
% this would be your protocol object, and you would inherit @soundmanager;
% (e.g., "obj = class(struct, 'myprotocol', soundmanager)". 
% But here, we're just exemplifying @soundmanager, so we won't use a 
% protocol. The simplest object that is also a @soundmanager is @soundmanager 
% itself:
gu = soundmanager

% Now start up the soundmanager:
SoundManagerSection(gu, 'init')

% Before making any sounds, let's get the sample rate:
srate = SoundManagerSection(gu, 'get_sample_rate')

% Let's declare a sound and make it a 1-second 2 Khz pure tone on the left speaker. The 
% 0.2 factor is just so it isn't too loud.
SoundManagerSection(gu, 'declare_new_sound', 'ptone', [0.2*sin(2*pi*2000*(1:srate)/srate) ; zeros(1, srate)])

% Let's hear it (this call will also automatically upload the sound to the 
% SoundServer):
SoundManagerSection(gu, 'play_sound', 'ptone');

% Let's change the sound with the name 'ptone' to a 1-sec, 4 KHz sound on 
% the right speaker:
SoundManagerSection(gu, 'set_sound', 'ptone', [zeros(1, srate) ; 0.2*sin(2*pi*4000*(1:srate)/srate)])

% If we want to upload it (so that, for example, it is in the SoundServer, 
% ready to be triggered by the StateMachine, we use this call:
SoundManagerSection(gu, 'send_not_yet_uploaded_sounds');

% If we want to know what integer id our sound is, so as to use 
% that when constructing a StateMachine that will trigger it:
SoundManagerSection(gu, 'get_sound_id', 'ptone')