Namespaces

Variants
Actions

MSP Sampling Tutorial 6: Record and Play Audio Files

From Cycling '74 Wiki
(Difference between revisions)
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
Click here to open the tutorial patch: [[06sRecordPlayAIFFFiles.maxpat]]
+
Click here to open the tutorial patch: [[Media:06sRecordPlayAIFFFiles.maxpat]]
  
 
===Playing from memory vs. playing from disk===
 
===Playing from memory vs. playing from disk===
  
You have already seen how to store sound in memory - in
+
You have already seen how to store sound in memory - in a {{maxword|name=buffer~}} - by recording into it directly or by reading in a pre-recorded audio file. Once the sound is in memory, it can be accessed in a variety of ways with {{maxword|name=cycle~}}, {{maxword|name=lookup~}}, {{maxword|name=index~}}, {{maxword|name=play~}}, {{maxword|name=groove~}}, {{maxword|name=wave~}}, etc.
a {{maxword|name=buffer~}} - by recording into it directly or by
+
reading in a pre-recorded audio file. Once the sound is
+
in memory, it can be accessed in a variety of ways
+
with {{maxword|name=cycle~}}, {{maxword|name=lookup~}}, {{maxword|name=index~}}, {{maxword|name=play~}}, {{maxword|name=groove~}}, {{maxword|name=wave~}}, etc.
+
  
The main limitation of {{maxword|name=buffer~}} for storing samples, of
+
The main limitation of {{maxword|name=buffer~}} for storing samples, of course, is the amount of unused RAM available on your computer. You can only store as much sound in memory as you have memory to hold it. For playing and recording very large amounts of audio data, it is more reasonable to use the hard disk for storage. But it takes more time to access the hard disk than to access RAM; therefore, even when playing from the hard disk, MSP still needs to create a small buffer to preload some of the sound into memory. That way, MSP can play the preloaded sound ''while'' it is getting more sound from the hard disk, without undue delay or discontinuities due to the time needed to access the disk.
course, is the amount of unused RAM available on your computer.
+
You can only store as much sound in memory as you have memory
+
to hold it. For playing and recording very large amounts of audio
+
data, it is more reasonable to use the hard disk for storage.
+
But it takes more time to access the hard disk than to access RAM;
+
therefore, even when playing from the hard disk, MSP still needs
+
to create a small buffer to preload some of the sound into memory.
+
That way, MSP can play the preloaded sound ''while'' it is
+
getting more sound from the hard disk, without undue delay or
+
discontinuities due to the time needed to access the disk.
+
  
 
===Record audio files: sfrecord~===
 
===Record audio files: sfrecord~===
  
MSP has objects for recording directly into, and playing directly
+
MSP has objects for recording directly into, and playing directly from, an audio file: {{maxword|name=sfrecord~}} and {{maxword|name=sfplay~}}. Recording an audio file is particularly easy, you just open a file, begin recording, and stop recording. (You don't even need to close the file; the {{maxword|name=sfrecord~}} object takes care of that for you.) In the upper right corner of the Patcher window there is a patch for recording files.
from, an audio file: {{maxword|name=sfrecord~}} and {{maxword|name=sfplay~}}. Recording
+
an audio file is particularly easy, you just open a file, begin recording,
+
and stop recording. (You don't even need to close the file; the
+
{{maxword|name=sfrecord~}} object takes care of that for you.) In the upper right
+
corner of the Patcher window there is a patch for recording files.
+
  
The {{maxword|name=sfrecord~}} object records to disk whatever signal data it
+
The {{maxword|name=sfrecord~}} object records to disk whatever signal data it receives in its inlets. The signal data can come directly from an {{maxword|name=adc~}} or {{maxword|name=ezadc~}} object, or from any other MSP object.
receives in its inlets. The signal data can come directly from
+
an {{maxword|name=adc~}} or {{maxword|name=ezadc~}} object, or from any other MSP object.
+
  
* Click on the {{maxword|name=message}} box marked ‘Create an AIFF file’. You will
+
* Click on the {{maxword|name=message}} box marked ‘Create an AIFF file’. You will be shown a Save As dialog box for naming your file. Navigate to the folder where you want to store the sound, name the file, and click Save. Turn audio on. Click on the {{maxword|name=toggle}} to begin recording; click on it again when you have finished.
be shown a Save As dialog box for naming your file. Navigate to the
+
folder where you want to store the sound, name the file, and click
+
Save. Turn audio on. Click on the {{maxword|name=toggle}} to begin recording;
+
click on it again when you have finished.
+
  
 
===Play audio files: sfplay~===
 
===Play audio files: sfplay~===
  
In the left part of the tutorial patcher there is a patch for playing
+
In the left part of the tutorial patcher there is a patch for playing audio files. The basic usage of {{maxword|name=sfplay~}} requires only a few objects, as shown in the following example. To play a file, you just have to open it and start {{maxword|name=sfplay~}}. The audio output of {{maxword|name=sfplay~}} can be sent directly to {{maxword|name=dac~}} or {{maxword|name=ezdac~}}, and/or anywhere else in MSP.
audio files. The basic usage of {{maxword|name=sfplay~}} requires only a few
+
objects, as shown in the following example. To play a file, you
+
just have to open it and start {{maxword|name=sfplay~}}. The audio output
+
of {{maxword|name=sfplay~}} can be sent directly to {{maxword|name=dac~}}
+
or {{maxword|name=ezdac~}}, and/or anywhere else in MSP.
+
  
* Click on the <code>open </code>{{maxword|name=message}} box marked ‘Set the current file’,
+
* Click on the <code>open </code>{{maxword|name=message}} box marked ‘Set the current file’, and open the audio file you have just recorded. Then (with audio on) click on the {{maxword|name=toggle}} marked ‘Play/Stop’ to hear your file.
and open the audio file you have just recorded. Then (with audio on)
+
click on the {{maxword|name=toggle}} marked ‘Play/Stop’ to hear your file.
+
  
 
===Play excerpts on cue===
 
===Play excerpts on cue===
  
Because {{maxword|name=sfplay~}} does not need to load an entire audio file into
+
Because {{maxword|name=sfplay~}} does not need to load an entire audio file into memory, you can actually have many files open in the same {{maxword|name=sfplay~}} object, and play any of them (or any portion of them) on cue. The most recently opened file is considered by {{maxword|name=sfplay~}} to be the ‘current’ file, and that is the file it will play when it receives the message <code>1</code>.
memory, you can actually have many files open in the same {{maxword|name=sfplay~}} object,
+
and play any of them (or any portion of them) on cue. The most recently
+
opened file is considered by {{maxword|name=sfplay~}} to be the ‘current’
+
file, and that is the file it will play when it receives the
+
message <code>1</code>.
+
  
* Click on the remaining <code>open </code>{{maxword|name=message}} boxes to open some other
+
* Click on the remaining <code>open </code>{{maxword|name=message}} boxes to open some other audio files, and then click on the {{maxword|name=message}} box marked ‘Define cues, 2 to 9’.
audio files, and then click on the {{maxword|name=message}} box
+
marked ‘Define cues, 2 to 9’.
+
  
The <code>preload</code> message to {{maxword|name=sfplay~}} specifies an entire file or a
+
The <code>preload</code> message to {{maxword|name=sfplay~}} specifies an entire file or a portion of a file, and assigns it a ''cue number''. From then on, every time {{maxword|name=sfplay~}} receives that number, it will play that cue. In the example patch, cues <code>2</code>, <code>3</code>, and <code>4</code> play entire files, cue <code>5</code> plays the first 270 milliseconds of ''sacre.aiff,'' and so on. Cue <code>1</code> is always reserved for playing the current (most recently opened) file, and cue <code>0</code> is reserved for stopping {{maxword|name=sfplay~}}.
portion of a file, and assigns it a ''cue number''. From then on, every
+
time {{maxword|name=sfplay~}} receives that number, it will play that cue. In the
+
example patch, cues <code>2</code>, <code>3</code>, and <code>4</code> play entire files,
+
cue <code>5</code> plays the first 270 milliseconds of ''sacre.aiff,'' and
+
so on. Cue <code>1</code> is always reserved for playing the current (most
+
recently opened) file, and cue <code>0</code> is reserved for stopping {{maxword|name=sfplay~}}.
+
  
Whenever {{maxword|name=sfplay~}} receives a cue, it stops whatever it is playing
+
Whenever {{maxword|name=sfplay~}} receives a cue, it stops whatever it is playing and immediately plays the new cue. (You can also send {{maxword|name=sfplay~}} a ''queue of cues'', by sending it a <code>list</code> of numbers, and it will play each cue in succession.) Each <code>preload</code> message actually creates a small buffer containing the audio data for the beginning of the cue, so playback can start immediately upon receipt of the cue number.
and immediately plays the new cue. (You can also send {{maxword|name=sfplay~}}
+
a ''queue of cues'', by sending it a <code>list</code> of numbers, and it
+
will play each cue in succession.) Each <code>preload</code> message actually
+
creates a small buffer containing the audio data for the beginning of
+
the cue, so playback can start immediately upon receipt of the cue number.
+
  
Now that cues 0 through 9 are defined, you can play different audio
+
Now that cues 0 through 9 are defined, you can play different audio excerpts by sending {{maxword|name=sfplay~}} those numbers. The upper-left portion of the patch permits you to type those numbers directly from the computer keyboard.
excerpts by sending {{maxword|name=sfplay~}} those numbers. The upper-left
+
portion of the patch permits you to type those numbers directly from
+
the computer keyboard.
+
  
* Click on the toggle marked ‘Keyplay On/Off’. Type number keys
+
* Click on the toggle marked ‘Keyplay On/Off’. Type number keys to play the different pre- defined cues. Turn ‘Keyplay’ off when you are done.
to play the different pre- defined cues. Turn ‘Keyplay’ off when you
+
are done.
+
  
 
===Try different file excerpts===
 
===Try different file excerpts===
  
Before you define a cue, you will probably need to listen to
+
Before you define a cue, you will probably need to listen to segments of the file to determine the precise start and end times you want. You can use the <code>seek</code> message to hear any segment of the current file.
segments of the file to determine the precise start and end times
+
you want. You can use the <code>seek</code> message to hear any segment
+
of the current file.
+
  
* Open your own audio file again (or any other audio file) to make it
+
* Open your own audio file again (or any other audio file) to make it the current file. In the right portion of this patch, enter an end time for the <code>seek</code> message. The excerpt you have specified will begin playing. Try different start and end times.
the current file. In the right portion of this patch, enter an end time for
+
the <code>seek</code> message. The excerpt you have specified will begin playing.
+
Try different start and end times.
+
  
Once you find start and end times you like, you could use them in
+
Once you find start and end times you like, you could use them in a <code>preload</code> message to establish a cue. Because {{maxword|name=sfplay~}} can't know in advance what excerpt it will be required to play in response to a <code>seek</code> message, it can't preload the excerpt. There will be a slight delay while it accesses the hard disk before it begins playing. For that reason, <code>seek</code> is best used as an auditioning tool; preloaded cues are better for performance situations where immediate playback is more critical.
a <code>preload</code> message to establish a cue. Because {{maxword|name=sfplay~}}
+
can't know in advance what excerpt it will be required to play in
+
response to a <code>seek</code> message, it can't preload the excerpt.
+
There will be a slight delay while it accesses the hard disk before
+
it begins playing. For that reason, <code>seek</code> is best used as an
+
auditioning tool; preloaded cues are better for performance
+
situations where immediate playback is more critical.
+
  
 
===Trigger an event at the end of a file===
 
===Trigger an event at the end of a file===
  
The patch in the lower right portion of the Patcher window demonstrates
+
The patch in the lower right portion of the Patcher window demonstrates the use of the right outlet of {{maxword|name=sfplay~}}. When a cue is done playing (or when it is stopped with a <code>0</code> message), {{maxword|name=sfplay~}} sends a <code>bang</code> out the right outlet. In this example patch, the <code>bang</code> is used to trigger the next (randomly chosen) cue, so {{maxword|name=sfplay~}} effectively restarts itself when each cue is done.
the use of the right outlet of {{maxword|name=sfplay~}}. When a cue is done
+
playing (or when it is stopped with a <code>0</code> message), {{maxword|name=sfplay~}}
+
sends a <code>bang</code> out the right outlet. In this example patch,
+
the <code>bang</code> is used to trigger the next (randomly chosen) cue,
+
so {{maxword|name=sfplay~}} effectively restarts itself when each cue is done.
+
  
Note the importance of the {{maxword|name=gate}} object in this patch. If it were
+
Note the importance of the {{maxword|name=gate}} object in this patch. If it were not present, there would be no way to stop {{maxword|name=sfplay~}} because each <code>0</code> cue would trigger another non-zero cue. The {{maxword|name=gate}} must be closed before the <code>0</code> cue is sent to {{maxword|name=sfplay~}}.
not present, there would be no way to stop {{maxword|name=sfplay~}} because
+
each <code>0</code> cue would trigger another non-zero cue. The {{maxword|name=gate}}
+
must be closed before the <code>0</code> cue is sent to {{maxword|name=sfplay~}}.
+
  
* In the patch marked ‘Play random excerpts’, click on the {{maxword|name=message}} box
+
* In the patch marked ‘Play random excerpts’, click on the {{maxword|name=message}} box to preload the cues, then click on the {{maxword|name=toggle}} to start the process. To stop it, click on the {{maxword|name=toggle}} again. Turn audio off.
to preload the cues, then click on the {{maxword|name=toggle}} to start the process. To
+
stop it, click on the {{maxword|name=toggle}} again. Turn audio off.
+
  
 
===Summary===
 
===Summary===
  
For large and/or numerous audio samples, it is often better
+
For large and/or numerous audio samples, it is often better to read the samples from the hard disk than to try to load them all into RAM. The objects {{maxword|name=sfrecord~}} and {{maxword|name=sfplay~}} provide a simple way to record and play audio files to and from the hard disk. The {{maxword|name=sfplay~}} object can have many audio files open at once. Using the <code>preload</code> message, you can pre-define ready cues for playing specific files or sections of files. The <code>seek</code> message to {{maxword|name=sfplay~}} lets you try different start and end points for a cue. When a cue is done playing (or is stopped) {{maxword|name=sfplay~}} sends a <code>bang</code> out its right outlet. This <code>bang</code> can be used to trigger other processes, including sending {{maxword|name=sfplay~}} its next cue.
to read the samples from the hard disk than to try to load
+
them all into RAM. The objects {{maxword|name=sfrecord~}} and {{maxword|name=sfplay~}}
+
provide a simple way to record and play audio files to and from
+
the hard disk. The {{maxword|name=sfplay~}} object can have many audio
+
files open at once. Using the <code>preload</code> message, you
+
can pre-define ready cues for playing specific files or
+
sections of files. The <code>seek</code> message to {{maxword|name=sfplay~}} lets
+
you try different start and end points for a cue. When a cue is
+
done playing (or is stopped) {{maxword|name=sfplay~}} sends a <code>bang</code>
+
out its right outlet. This <code>bang</code> can be used to trigger
+
other processes, including sending {{maxword|name=sfplay~}} its next cue.
+
  
 
===See Also===
 
===See Also===

Latest revision as of 15:51, 28 June 2012

Click here to open the tutorial patch: Media:06sRecordPlayAIFFFiles.maxpat

Contents

[edit] Playing from memory vs. playing from disk

You have already seen how to store sound in memory - in a buffer~ - by recording into it directly or by reading in a pre-recorded audio file. Once the sound is in memory, it can be accessed in a variety of ways with cycle~, lookup~, index~, play~, groove~, wave~, etc.

The main limitation of buffer~ for storing samples, of course, is the amount of unused RAM available on your computer. You can only store as much sound in memory as you have memory to hold it. For playing and recording very large amounts of audio data, it is more reasonable to use the hard disk for storage. But it takes more time to access the hard disk than to access RAM; therefore, even when playing from the hard disk, MSP still needs to create a small buffer to preload some of the sound into memory. That way, MSP can play the preloaded sound while it is getting more sound from the hard disk, without undue delay or discontinuities due to the time needed to access the disk.

[edit] Record audio files: sfrecord~

MSP has objects for recording directly into, and playing directly from, an audio file: sfrecord~ and sfplay~. Recording an audio file is particularly easy, you just open a file, begin recording, and stop recording. (You don't even need to close the file; the sfrecord~ object takes care of that for you.) In the upper right corner of the Patcher window there is a patch for recording files.

The sfrecord~ object records to disk whatever signal data it receives in its inlets. The signal data can come directly from an adc~ or ezadc~ object, or from any other MSP object.

  • Click on the message box marked ‘Create an AIFF file’. You will be shown a Save As dialog box for naming your file. Navigate to the folder where you want to store the sound, name the file, and click Save. Turn audio on. Click on the toggle to begin recording; click on it again when you have finished.

[edit] Play audio files: sfplay~

In the left part of the tutorial patcher there is a patch for playing audio files. The basic usage of sfplay~ requires only a few objects, as shown in the following example. To play a file, you just have to open it and start sfplay~. The audio output of sfplay~ can be sent directly to dac~ or ezdac~, and/or anywhere else in MSP.

  • Click on the open message box marked ‘Set the current file’, and open the audio file you have just recorded. Then (with audio on) click on the toggle marked ‘Play/Stop’ to hear your file.

[edit] Play excerpts on cue

Because sfplay~ does not need to load an entire audio file into memory, you can actually have many files open in the same sfplay~ object, and play any of them (or any portion of them) on cue. The most recently opened file is considered by sfplay~ to be the ‘current’ file, and that is the file it will play when it receives the message 1.

  • Click on the remaining open message boxes to open some other audio files, and then click on the message box marked ‘Define cues, 2 to 9’.

The preload message to sfplay~ specifies an entire file or a portion of a file, and assigns it a cue number. From then on, every time sfplay~ receives that number, it will play that cue. In the example patch, cues 2, 3, and 4 play entire files, cue 5 plays the first 270 milliseconds of sacre.aiff, and so on. Cue 1 is always reserved for playing the current (most recently opened) file, and cue 0 is reserved for stopping sfplay~.

Whenever sfplay~ receives a cue, it stops whatever it is playing and immediately plays the new cue. (You can also send sfplay~ a queue of cues, by sending it a list of numbers, and it will play each cue in succession.) Each preload message actually creates a small buffer containing the audio data for the beginning of the cue, so playback can start immediately upon receipt of the cue number.

Now that cues 0 through 9 are defined, you can play different audio excerpts by sending sfplay~ those numbers. The upper-left portion of the patch permits you to type those numbers directly from the computer keyboard.

  • Click on the toggle marked ‘Keyplay On/Off’. Type number keys to play the different pre- defined cues. Turn ‘Keyplay’ off when you are done.

[edit] Try different file excerpts

Before you define a cue, you will probably need to listen to segments of the file to determine the precise start and end times you want. You can use the seek message to hear any segment of the current file.

  • Open your own audio file again (or any other audio file) to make it the current file. In the right portion of this patch, enter an end time for the seek message. The excerpt you have specified will begin playing. Try different start and end times.

Once you find start and end times you like, you could use them in a preload message to establish a cue. Because sfplay~ can't know in advance what excerpt it will be required to play in response to a seek message, it can't preload the excerpt. There will be a slight delay while it accesses the hard disk before it begins playing. For that reason, seek is best used as an auditioning tool; preloaded cues are better for performance situations where immediate playback is more critical.

[edit] Trigger an event at the end of a file

The patch in the lower right portion of the Patcher window demonstrates the use of the right outlet of sfplay~. When a cue is done playing (or when it is stopped with a 0 message), sfplay~ sends a bang out the right outlet. In this example patch, the bang is used to trigger the next (randomly chosen) cue, so sfplay~ effectively restarts itself when each cue is done.

Note the importance of the gate object in this patch. If it were not present, there would be no way to stop sfplay~ because each 0 cue would trigger another non-zero cue. The gate must be closed before the 0 cue is sent to sfplay~.

  • In the patch marked ‘Play random excerpts’, click on the message box to preload the cues, then click on the toggle to start the process. To stop it, click on the toggle again. Turn audio off.

[edit] Summary

For large and/or numerous audio samples, it is often better to read the samples from the hard disk than to try to load them all into RAM. The objects sfrecord~ and sfplay~ provide a simple way to record and play audio files to and from the hard disk. The sfplay~ object can have many audio files open at once. Using the preload message, you can pre-define ready cues for playing specific files or sections of files. The seek message to sfplay~ lets you try different start and end points for a cue. When a cue is done playing (or is stopped) sfplay~ sends a bang out its right outlet. This bang can be used to trigger other processes, including sending sfplay~ its next cue.

[edit] See Also

sfplay~ - Play audio file from disk

sfrecord~ - Record to audio file on disk