SoundFile:
Filter:
Classes | Files

SoundFile : Object

sclang soundfile data
Source: SoundFile.sc

Description

The SoundFile class is used to check the size, format, channels etc. when the sclang client needs this information about a SoundFile. Soundfile data can be read and modified. Soundfile data can also be read and written incrementally, so with properly designed code, there is no restriction on the file size.

In most cases you will wish to send commands to the server to get it to load SoundFiles directly into Buffers. You will not need to use this class for this. See the Buffer helpfile.

When reading a sound file, the headerFormat, sampleFormat, numChannels and numFrames variables will be set according to the file on disk.

When creating a new SoundFile, the format will be monophonic, 44.1 kHz, AIFF, floating-point by default. Users may override the defaults by passing the desired format strings to SoundFile: *openWrite, or by using SoundFile: -headerFormat, SoundFile: -sampleFormat, SoundFile: -numChannels and SoundFile: -sampleRate before calling SoundFile: -openWrite.

Class Methods

SoundFile.new(pathName)

Creates a new SoundFile instance.

SoundFile.openRead(pathName)

Try to open the audio file at the given path.

Arguments:

pathName

Full path to the sound file. Use String: -standardizePath to resolve home-folder shortcuts such as ~.

Returns:

A new SoundFile instance if successful, or nil if file open failed. User code should check for nil before doing anything with the SoundFile object.

SoundFile.openWrite(pathName, headerFormat, sampleFormat, numChannels, sampleRate)

Try to create an audio file at the given path. Note that there is no numFrames argument: the number of frames is counted after writing data into the file.

Arguments:

pathName

Full path to the sound file. Use String: -standardizePath to resolve home-folder shortcuts such as ~.

headerFormat

A string for the sound file format. Valid strings are listed at SoundFile: -headerFormat. If not given, the default "AIFF" is used.

sampleFormat

A string for the sample format. Valid strings are listed at SoundFile: -sampleFormat. If not given, the default "float" is used.

numChannels

An integer number of channels (1 by default).

sampleRate

An integer sample rate (44100 by default).

Returns:

A new SoundFile instance if successful, or nil if file open failed. User code should check for nil before doing anything with the SoundFile object.

SoundFile.collect(path: "sounds/*")

Returns an Array of SoundFile objects whose paths match the pattern. (The associated files are closed. These objects can be used to cue playback buffers)

SoundFile.use(path, function)

Reads the data of a SoundFile, evaluates the function (passing the file as argument) and closes it again.

SoundFile.normalize(path, outPath, newHeaderFormat, newSampleFormat, startFrame: 0, numFrames, maxAmp: 1.0, linkChannels: true, chunkSize: 4194304, threaded: false)

Normalizes a soundfile to a level set by the user. The normalized audio will be written into a second file.

Using this class method (SoundFile.normalize) will automatically open the source file for you. You may also -openRead the SoundFile yourself and call -normalize on it. In that case, the source path is omitted because the file is already open.

See instance method -normalize for more information.

Inherited class methods

Undocumented class methods

SoundFile.closeAll

SoundFile.collectIntoBuffers(path: "sounds/*", server)

SoundFile.groupNormalize(paths, outDir, newHeaderFormat, newSampleFormat, maxAmp: 1.0, chunkSize: 4194304, threaded: true)

SoundFile.openFiles

Instance Methods

Playback

.cue(ev, playNow: false, closeWhenDone: false)

Allocates a buffer and cues the SoundFile for playback. Returns an event parameterized to play that buffer. (See NodeEvent for a description of how events can be used to control running synths.) The event responds to play, stop, pause, resume, keeping the buffer open. The buffer is closed when the event is sent a close message.

Arguments:

ev

An Event can passed as an argument allowing playback to be customized using the following keys:

keydefault valuewhat it does
bufferSize65536Must be a power of two (65536, 131072 or 262144 recommended)
firstFrame0first frame to play
lastFramenillast frame to play (nil plays to end of file)
out:0sets output bus
server:Server.defaultwhich server
group:1what target
addAction:0head/tail/before/after
amp:1amplitude
instrument:nilif nil SoundFile:cue determines the SynthDef (one of diskIn1, diskIn2, ...diskIn16)

Where bufferSize, firstFrame, lastFrame are for buffer and playback position, and out, server, group, addAction, amp are synth parameters. Here is the default SynthDef used for stereo files:

The control sustainTime determines playback duration based on the firstFrame and lastFrame. The control gate allows early termination of the playback

playNow

This is a Boolean that determines whether the file is to be played immediately after cueing.

closeWhenDone

A flag to indicate whether the buffer will be closed after playback is finished. Default is False.

Read/Write

.openRead(pathName)

Read the header of a file. Answers a Boolean whether the read was successful. Sets the -numFrames, -numChannels and -sampleRate. Does not set the -headerFormat and -sampleFormat.

Arguments:

pathName

a String specifying the path name of the file to read.

.readData(rawArray)

Reads the sample data of the file into the raw array you supply. You must have already called -openRead.

When you reach EOF, the array's size will be 0. Checking the array size is an effective termination condition when looping through a sound file. See the method -channelPeaks for example.

Arguments:

rawArray

The raw array must be a FloatArray. Regardless of the sample format of the file, the array will be populated with floating point values. For integer formats, the floats will all be in the range -1..1.

The size of the FloatArray determines the maximum number of single samples (not sample frames) that will be read. If there are not enough samples left in the file, the size of the array after the readData call will be less than the original size.

.openWrite(pathName)

Write the header of a file. Answers a Boolean whether the write was successful.

Arguments:

pathName

a String specifying the path name of the file to write.

.writeData(rawArray)

Writes the rawArray to the sample data of the file. You must have already called -openWrite.

Arguments:

rawArray

The raw array must be a FloatArray or Signal, with all values between -1 and 1 to avoid clipping during playback.

.isOpen

answers if the file is open.

.close

closes the file.

.duration

the duration in seconds of the file.

Normalizing

.normalize(outPath, newHeaderFormat, newSampleFormat, startFrame: 0, numFrames, maxAmp: 1.0, linkChannels: true, chunkSize: 4194304, threaded: false)

Normalizes a soundfile to a level set by the user. The normalized audio will be written into a second file.

The normalizer may be used to convert a soundfile from one sample format to another (e.g., to take a floating point soundfile produced by SuperCollider and produce an int16 or int24 soundfile suitable for use in other applications).

NOTE: While the normalizer is working, there is no feedback to the user. It will look like SuperCollider is hung, but it will eventually complete the operation. You can set threaded:true to get feedback but it will take slightly longer to complete.

Arguments:

outPath

a path to the destination file.

newHeaderFormat

the desired header format of the new file; if not specified, the header format of the source file will be used.

newSampleFormat

the desired sample format of the new file; if not specified, the sample format of the source file will be used.

startFrame

an index to the sample frame to start normalizing.

numFrames

the number of sample frames to copy into the destination file (default nil, or entire soundfile).

maxAmp

the desired maximum amplitude. Provide a floating point number or, if desired, an array to specify a different level for each channel.

linkChannels

a Boolean specifying whether all channels should be scaled by the same amount. The default is true, meaning that the peak calculation will be based on the largest sample in any channel. If false, each channel's peak will be calculated independently and all channels will be scaled to maxAmp (this would alter the relative loudness of each channel).

chunkSize

how many samples to read at once (default is 4194304, or 16 MB).

threaded

if true, the normalization runs in a routine so that SC can respond (intermittently) while processing. Prevents macOS beachballing.

Instance Variables

.path

.path = value

Get the pathname of the file. This variable is set via the -openRead or -openWrite calls.

.headerFormat

.headerFormat = value

This is a String indicating the header format which was read by openRead and will be written by openWrite. In order to write a file with a certain header format you set this variable.

read/write header formats:
"AIFF"Apple/SGI AIFF format
"WAV","WAVE", "RIFF"Microsoft WAV format
"Sun", "NeXT"Sun/NeXT AU format
"SD2"Sound Designer 2
"IRCAM"Berkeley/IRCAM/CARL
"raw"no header = raw data
"MAT4"Matlab (tm) V4.2 / GNU Octave 2.0
"MAT5"Matlab (tm) V5.0 / GNU Octave 2.1
"PAF"Ensoniq PARIS file format
"SVX"Amiga IFF / SVX8 / SV16 format
"NIST"Sphere NIST format
"VOC"VOC files
"W64"Sonic Foundry's 64 bit RIFF/WAV
"PVF"Portable Voice Format
"XI"Fasttracker 2 Extended Instrument
"HTK"HMM Tool Kit format
"SDS"Midi Sample Dump Standard
"AVR"Audio Visual Research
"FLAC"FLAC lossless file format
"CAF"Core Audio File format

Additionally, a huge number of other formats are supported read only. Please note that WAV file support is limited to 4GB. For output of multiple channels or very long recordings we suggest to use W64 (or CAF on macOS).

.sampleFormat

.sampleFormat = value

A String indicating the format of the sample data which was read by -openRead and will be written by -openWrite. libsndfile determines which header formats support which sample formats. This information is detailed at http://www.mega-nerd.com/libsndfile . The possible header formats are:

sample formats:
"int8", "int16", "int24", "int32"
"mulaw", "alaw",
"float"

Not all header formats support all sample formats.

.numFrames

The number of sample frames in the file.

.numChannels

.numChannels = value

The number of channels in the file.

.sampleRate

.sampleRate = value

The sample rate of the file.

Inherited instance methods

Undocumented instance methods

.asBuffer(server)

.asEvent(type: 'allocRead')

.channelPeaks(startFrame: 0, numFrames, chunkSize: 1048576, threaded: false)

.fileptr

.fileptr = value

.info(path)

.play(ev, playNow: true)

.readHeaderAsString

.scaleAndWrite(outFile, scale, startFrame, numFrames, chunkSize, threaded: false)

.seek(offset: 0, origin: 0)

.toCSV(outpath, headers, delim: ",", append: false, func, action)

Examples