Creare un file WAV da un file .orc e un file .sco di Csound con le funzioni esterne del API di libcsound64

Da Gambas-it.org - Wikipedia.
Versione del 1 giu 2017 alle 09:59 di Vuott (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Csound è un linguaggio di programmazione, scritto in C, per il suono. Esso si basa su un'unità che genera suono modificabile dall'utente. Csound si serve di due file di testo appositamente formattati: il file "orchestra" (.orc) ed il file "score" (.sco). Il file "orchestra" descrive la natura degli strumenti e dice al computer "come" sintetizzare il suono, mentre il file "score" descrive le note e gli altri parametri lungo una linea temporale e dice quindi al computer "quando" eseguire quelle note e gli altri elementi dell'audio. Csound elabora le istruzioni contenute in questi file e restituisce un file audio o un flusso audio in tempo reale.


Mostreremo di seguito un breve codice che utilizza un file .orc ed un file .sco per generare in uscita il corrispondente file audio di tipo .wav . Per poter ottenere questo risultato, faremo uso della libreria (nella sua attuale versione): libcsound64

Library "libcsound64"

' "CSOUND* csoundCreate (void * hostData)
' Creates an instance of Csound. Returns an opaque pointer that must be passed to most Csound API functions.
Private Extern csoundCreate(hostData As Pointer) As Pointer

' int csoundCompile (CSOUND *, int argc, char **argv)
' Compiles Csound input files (such as an orchestra and score)
' As Directed by the supplied command - Line arguments, but does Not perform them.
Private Extern csoundCompile(csP As Pointer, argc As Integer, argv As String[]) As Integer

' int csoundPerformKsmps (CSOUND *)
' Senses input events, and performs one control sample worth (ksmps) of audio output.
Private Extern csoundPerformKsmps(csP As Pointer) As Integer

' int csoundCleanup (CSOUND *)
' Prints information about the end of a performance, and closes audio and MIDI devices.
Private Extern csoundCleanup(csP As Pointer) As Integer

' void csoundDestroy (CSOUND *)
' Destroys an instance Of Csound.
Private Extern csoundDestroy(csP As Pointer)


Public Sub Main()

 Dim csound As Pointer
 Dim result, argc As Integer
 Dim argv As String[]
 

' Crea un oggetto "CSound":
   csound = csoundCreate(0)
   
   argc = 3
   
   argv = [" ", "percorso/file.orc", "/percorso/file.sco"]
   
' Esegue un ciclo per compilare i due file in entrata e creare il file wav in uscita:
   result = csoundCompile(csound, argc, argv)
   if result <> 0 then Error.Raise("Errore alla funzione 'csoundCompile()' !")

   If Not result Then
     While csoundPerformKsmps(csound) = 0
     Wend
     csoundCleanup(csound)
   Endif
   
' Distrugge l'oggetto "Csound":
   csoundDestroy(csound)

End

Il file wav di uscita, denominato automaticamente "test.wav", sarà generato nel percorso dell'applicazione Gambas.


Riferimenti

[1] Il sito di Csound

[2] L'API di Csound

[3] Eastman Csound Tutorial