The Recorder class
- class audiomath.Recorder(seconds, device=None, stream=None, bufferLengthMsec=None, minLatencyMsec=None, fs=None, start=True, loop=False, verbose=None, filename=None, **kwargs)
Bases:
GenericRecorderA
Recorderprovides a persistent connection to the chosen recording hardware, allowing sound to be recorded into aSoundinstance.You may find it more useful to use the global function
Record, which synchronously records and returns aSoundinstance, than to create or interact directly withRecorderinstances. However,Recorderinstances are the way to go if you want to record asynchronously, in the background.- Parameters:
seconds (float, Sound) – number of seconds to pre-allocate for recording, or an already-pre-allocated
Soundinstance into which to recorddevice (int, str, dict, Stream) – specification of the device/stream to use for recording (as an index, name, full device record from
FindDevice(), or already-openStreaminstance)stream (int, str, dict, Stream) – synonymous with
device, for compatibilityfs (float, None) – Optionally specify the sampling frequency, in Hz, when creating your first
Recorderor firstStream(after that,Recorderinstances may share an openStreaminstance so it is possible that only the first call will make any difference).start (bool) – whether to start recording immediately
loop (bool) – whether to record indefinitely, treating
self.soundas a circular buffer, or simply stop when the capacity ofself.soundis reachedverbose (bool, None) – verbosity for debugging. If
None, inherit from the setting specified bySetDefaultVerbosity(), if any**kwargs – passed through to
Set()to initialize properties of theRecorderinstance.
- classmethod MakeRecording(space=60, prompt=None, verbose=None, cut=True, nChannels=None, filename=None, **kwargs)
Record and return a
Soundsynchronously. Slightly easier than creating your ownRecorderinstance and working with that (which is what you would have to do if you want to record asynchronously).- Parameters:
space (Sound, float) – specifies either a pre-existing
Soundinstance into which to record, or a number of seconds. In the latter case aSoundinstance is created, pre- allocated with the specified amount of space in seconds.prompt (None, bool, str, function) – if this is left as
None, a default string is composed. Ifpromptis a string, it is printed to the console and theRecorderwaits until it has finished (or until ctrl-C is pressed). Alternatively, supply a callable whose non-Nonereturn value signals that the recording should end.verbose (bool) – Passed through to the constructor of the
Recorderobject that is being used.cut (bool) – If true, return a
Cut()version of theSoundwhen recording stops—i.e. ensure that the duration of theSounddoes not exceed the duration of the recording. If false, the entire pre-allocatedspaceis returned, with the recording at the beginning.nChannels (int) – Optionally specify the number of channels to record (some APIs have crazy defaults, like ALSA’s 32 channels - if you’re not using one of those APIs, you probably don’t need this).
filename (str) – Optionally, use the
ffmpegclass to stream the recorded content to the specified file. This requires theffmpegbinary to be (separately) installed—see theffmpegclass documentation.**kwargs – Additional keyword arguments are passed through to the constructor of the
Recorderobject (for example, to specify thedevicethat should be used).
Examples:
import audiomath as am s = am.Record() # this records for up to 60 seconds (less if ctrl-C # is pressed) and returns a `Sound` instance containing # the recorded data. s2 = am.Record(10, loop=True, filename='blah.mp3') # this records indefinitely into a 10-second circular # buffer, streaming the recorded data all the while into # the file `blah.mp3`, and when you press ctrl-C it # stops, cleans up, and returns the last 10 seconds as # a `Sound` instance.
- Cut(position=None)
Stop recording, and return the portion of
self.soundthat has been recorded so far, up to the currentheadposition (or the explicitly specifiedpositionin seconds), as a newSoundinstance.
- Pause(position=None, **kwargs)
Stop recording. If
positionis specified, move the recordingheadto that position, expressed in seconds from the beginning.
- ReadSamples(startPositionInSamples, nSamples)
Read the specified number of samples and return them as a
numpyarray. If necessary, wait until the requisite number of samples has been recorded. Ifself.loop==False, then this method may return fewer samples than you ask for if the recording reaches, or has already reached, the end of the space allocated inself.sound. Otherwise, the wraparound calculations will be handled automatically and the return value will have the requested number of samples in chronological order.
- Record(position=None, wait=False, **kwargs)
Start recording. If
positionis specified, move the recordingheadto that position, expressed in seconds from the beginning, before starting. Ifwaitis true,Wait()until the end, or until the user presses ctrl+C.
- Seek(position, relative=False)
Move the recording
headto the specifiedpositionin seconds. Negative values count back from the end of the available recording space inself.sound. Ifrelativeis true,positionis interpreted as an offset relative to the current head position. The following are equivalent:r.Seek( 5.0, relative=False ) # more efficient r.head = 5.0
And the following are also equivalent:
r.Seek( -2.0, relative=True ) # more efficient r.head -= 2.0
- Set(**kwargs)
Set the values of multiple attributes or properties in one call. An error will be raised if you try to set the value of a non-existent attribute.
- Returns:
self
Example:
p.Set( head=0, recording=True )
- Start(position=None, wait=False, **kwargs)
Start recording. If
positionis specified, move the recordingheadto that position, expressed in seconds from the beginning, before starting. Ifwaitis true,Wait()until the end, or until the user presses ctrl+C.
- Stop(position=None, **kwargs)
Stop recording. If
positionis specified, move the recordingheadto that position, expressed in seconds from the beginning.
- Wait()
Sleep until
self.recordingis no longer true. This will occur when the allocated recording time is exhausted, or if the user presses ctrl+C in the meantime.
- WaitFor(condition, finalRecordingStatus=None)
Sleep until
conditionis fulfilled. Theconditionargument may be:- a
Recorderinstance: in which case the method waits until the recorder is no longer recording;
- a
Playerinstance: in which case the method waits until the player is no longer playing;
- a string:
in which case the method will print the string as a prompt and wait for the user to enter press ctrl-C on the console;
- any callable:
in which case the method will repeatedly call
condition()between 1-ms sleeps until it returns anything other thanNone.
If
finalRecordingStatusis notNone, thenself.recordingis set to its value when the wait is over. So for example, you can prompt a user to end an ongoing recording as follows:r.WaitFor('Press ctrl-C to stop recording: ', finalRecordingStatus=False)
- a
- property head
Current recording position, in seconds relative to the beginning.
- property loop
A boolean property. If this is
True, treatself.soundas a circular buffer and record into it indefinitely (use theCut() method to extract the recorded data in the correct order). If this is `False, the recorder will simply stop recording when the capacity ofself.soundis reached.