RECORD AND PLAY SOUNDFILES
Playing Soundfiles From Disk - diskin2
The simplest way of playing a sound file from Csound is to use the diskin2 opcode. This opcode reads audio directly from the hard drive location where it is stored, i.e. it does not pre-load the sound file at initialization time. This method of sound file playback is therefore good for playing back very long, or parts of very long, sound files. It is perhaps less well suited to playing back sound files where dense polyphony, multiple iterations and rapid random access to the file is required. In these situations reading from a function table or buffer is preferable.
diskin2 has additional parameters for speed of playback, and interpolation; these will be discussed in the section playback speed and direction.
<CsoundSynthesizer> <CsOptions> -odac </CsOptions> <CsInstruments> ;example written by Iain McCurdy sr = 44100 ksmps = 32 nchnls = 1 instr 1; play audio from disk using diskin2 opcode kSpeed init 1; playback speed iSkip init 0; inskip into file (in seconds) iLoop init 0; looping switch (0=off 1=on) ; READ AUDIO FROM DISK a1 diskin2 "loop.wav", kSpeed, iSkip, iLoop out a1; send audio to outputs endin </CsInstruments> <CsScore> i 1 0 6 e </CsScore> </CsoundSynthesizer>
Writing Audio to Disk
The traditional method of rendering Csound's audio to disk is to specify a sound file as the audio destination in the Csound command or under <CsOptions>, in fact before real-time performance became a possibility this was the only way in which Csound was used. With this method, all audio that is piped to the output using out, outs etc. will be written to this file. The number of channels that the file will conatain will be determined by the number of channels specified in the orchestra header using 'nchnls'. The disadvantage of this method is that we cannot simultaneously listen to the audio in real-time.
<CsoundSynthesizer> <CsOptions> ;audio output destinatoin is given as a sound file (wav format specified) ;audio destination cannot simultaneously be live output when using this method to record -oWriteToDisk1.wav -W </CsOptions> <CsInstruments> ;example written by Iain McCurdy sr = 44100 ksmps = 32 nchnls = 1 0dbfs = 1 giSine ftgen 0, 0, 4096, 10, 1 ; a sine wave instr 1; a simple tone generator aEnv expon 0.2, p3, 0.001; a percussive amplitude envelope aSig poscil aEnv, cpsmidinn(p4), giSine; audio oscillator out aSig; send audio to output endin </CsInstruments> <CsScore> ; two chords i 1 0 5 60 i 1 0.1 5 65 i 1 0.2 5 67 i 1 0.3 5 71 i 1 3 5 65 i 1 3.1 5 67 i 1 3.2 5 73 i 1 3.3 5 78 e </CsScore> </CsoundSynthesizer>
Writing Audio to Disk with Simultaneous Reatime Audio Output - fout
The newer method and of writing audio to a sound file which permits simultaneous real time output is the use of the fout opcode. Because this opcode is used within the orchestra we have a little more work to do to ensire that all the audio generated by Csound is piped through fout, but the advantage is that we have many more options with regard to how and when this is done. With fout we can choose between many different file formats, details of these can be found in fout's entry in the Csound Manual.
<CsoundSynthesizer> <CsOptions> -odac </CsOptions> <CsInstruments> ;example written by Iain McCurdy sr = 44100 ksmps = 32 nchnls = 1 0dbfs = 1 giSine ftgen 0, 0, 4096, 10, 1 ; a sine wave gaSig init 0; set initial value for global audio variable (silence) instr 1; a simple tone generator aEnv expon 0.2, p3, 0.001; percussive amplitude envelope aSig poscil aEnv, cpsmidinn(p4), giSine; audio oscillator gaSig = gaSig + aSig; accumulate this note with the global audio variable endin instr 2; write to a file (always on) ; USE FOUT TO WRITE TO A FILE ON DISK ; FORMAT 4 RESULTS IN A 16BIT WAV ; NUMBER OF CHANNELS IS DETERMINED BY THE NUMBER OF AUDIO VARIABLES SUPPLIED TO fout fout "WriteToDisk2.wav", 4, gaSig out gaSig; send audio for all notes combined to the output clear gaSig; clear global audio variable to prevent run away accumulation endin </CsInstruments> <CsScore> ; activate recording instrument to encapsulate the entire performance i 2 0 8.3 ; two chords i 1 0 5 60 i 1 0.1 5 65 i 1 0.2 5 67 i 1 0.3 5 71 i 1 3 5 65 i 1 3.1 5 67 i 1 3.2 5 73 i 1 3.3 5 78 e </CsScore> </CsoundSynthesizer>