Contents |
import gpSoundPlayerThe module defines the classes and functions that provide an easy interface to the sound player. The module also defines some utility functions.
p = gpSoundPlayer.SoundPlayer()A connection to the player program is established with the connect() method of the player ohject (using default values):
p.connect()The player program is expected to be running at this time and waiting for a connection. Connect() may be given two arguments, a hostname and port number telling where to connect instead of the defaults, for example:
p.connect('bekesy.hut.fi', 10000)Alternatively, the launch() method can be used to automatically start the player program and connect to it:
p.launch()Launch() first forks a new process and then exec's the player program. Then the player module connects to the new player process with the connect() method. Launch() takes the sampling rate and number of output channels from the player's variables p.rate and p.channels. These can be set before using launch() to set the desired values.
See also: terminating the player
Before any new sample objects are created the sample class has to be told which player instance to use:
gpSoundPlayer.sample.SoundPlayer = pSamples created after that use that player for playing the samples. It is therefore, in theory, possible to have multiple players at one time each with it's own set of samples.
New sample objects are created by making instances of the derived classes gpSoundPlayer.filesample and gpSoundPlayer.nullsample.
s1 = gpSoundPlayer.filesample('audiofiles/BBintro.aiff')Now s1 is a new sample instance. Raw data samples can be loaded by adding two optional arguments:
s2 = gpSoundPlayer.filesample('audiofiles/sample_data', channels, endian)where channels is an integer telling how many channels there are in the sample and endian is a string, either 'LITTLE_ENDIAN' or 'BIG_ENDIAN', which specifies the byteorder of the data. Endian can be omitted in which case the endianess of the data is assumed to be the same as the host's.
Null samples are similarly created by calling the gpSoundPlayer.nullsample class with the length of the sample as argument:
s3 = gpSoundPlayer.nullsample(44100)where in this case, if the sampling rate were 44.1KHz, the sample s3 would be a one second sample of silence.
s1.kill()The sample data is freed in the player and the sample is no longer usable. Deleting a sample object in python doesn't unload the sample (there were some problems).
A sample that has been killed can be reloaded with the reload() method:
s1.reload()After that the sample is again usable.
s.start() s.start(s2, s3, ...) s.stop() t = s.play() t = s.play(s2, s3, ...) m = s.Play() m = s.Play(s2, s3, ...)The s.start() command starts playing the sample and returns immediately. The start() method can be given any number of other sample ohjects as parameters. This means that the samples will be started at the same time.
s.stop() stops the sample. If the sample was not playing it has no effect.
s.play() plays the sample and returns after the sample has been played. It returns the time when the sample ended (struct timeval converted to a float). When more samples are given as parameters they are started at the same time, as in start(). However, the function returns when the sample s has been played. The parameter-samples may or may not have ended at that time. (This might seem strange but it is the way it is now). Other version of play() is Play() which returns immediately a message. This message can be used to wait for the reply to this message, ie. the notification that the sample has stopped. (Waiting for messages and other more advanced features will not be discussed at this point).
s.setvolume(vol) s.setvolume((chan, vol), (chan, vol), ...)There are two alternative forms: The first sets the volume level of all the channels in the sample to vol (floating point number). The second form takes any number of (channel, vol) tuples where channel is the channel number and vol is the volume and sets the volumes of the channels accordingly.
Examples: 1. set volume of sample s1 (all channels) to 20dB below the samples normal level:s1.setvolume(gpSoundPlayer.dB(-20))(the gpSoundPlayer.dB() is a function that converts decibels to the values the player wants.2. set channels' volumes separately:
s1.setvolume((0, 0.1), (1, 0.2))which sets channel 0's volume to 0.1 (-20dB) and channel 1's to 0.2 (about -14dB).
s.setvolume((chan, output, vol), (chan, output, vol), ...)Setvolume() takes any number of (channel, output, vol) tuples where channel is the channel number, output is the output channel number and vol is the volume and sets the connections accordingly: Channel chan is connected to output channel output with weight vol. The meaning of the parameters are similar to those in the volume control.
s.setpointer(pointer) s.setpointer((channel, pointer), (channel, pointer), ...) s.movepointer(pointer) s.movepointer((channel, pointer), (channel, pointer), ...) s.set_initpointer(pointer) s.set_initpointer((channel, pointer), (channel, pointer), ...)Where pointer is an integer number. The alternative forms work exactly the same way as with the volume control.
setpointer() sets the pointer to a value. movepointer() adds a value to the current sample pointer.
Setting the sample pointer to a negative value is allowed in which case the sample outputs silence until the pointer reaches zero and then begins outputting the sample data.
Set_initpointer() is used to set the 'initial pointer' of a channel. When the sample is stopped or ends the sample pointer is reset from this value. It can be used to set a delay before the sample starts (using a negative value for this pointer) or to start it from some point in the sample. When the sample is set to loop, the sample pointer is reset to 0 when the end of sample is reached regardless of the value of the initial pointer.
s1.looping(1) s1.looping(0)The first one turns looping on, the second turns it off. To play the looping sample, use the start() method and to stop, use stop(). Using play() with a looping sample is considered illegal (play() returns when the sample stops. When does a looping sample stop?)
p.setvolume(vol) p.setvolume((ochan, vol), (ochan, vol), ...)The method is very similar to the sample volume control, except that there is a player involved (not a sample) and the channel number is now output channel number. This command would be used to set the output to the 'most comfortable listening level' at the beginning of the test.
p.quit()This tells the player to terminate and then closes the connection. If the player was started with the launch() method, the module wait's for the player process to exit before returning. A new connection can be established with the connect() or launch() methods.