/* Einfache Audio-Ausgabe mit ML:
   Daten befinden sich in audioBuffer */


#include <ML/ml.h>
#include <ML/mlu.h>


MLpv msg[5];    /* Message-Buffer */

/* 1. passenden Ausgabe-Jack finden */

MLint64 devId=0;
MLint64 jackId=0;
mluFindDeviceByName( ML_SYSTEM_LOCALHOST, "audio device", &devId );
mluFindFirstOutputJack( devId, &jackId );

/* 2. Pfad zum Jack aufbauen */

MLint64 pathId=0;
MLopenid openPath;
mluFindPathToJack( jackId, &pathId );
mlOpen( pathId, NULL, &openPath ); 

/* 3. Pfad konfigurieren */

msg[0].param = ML_AUDIO_FORMAT_INT32;
msg[0].value.int32 = ML_FORMAT_S16;

msg[1].param = ML_AUDIO_CHANNELS_INT32;
msg[1].value.int32 = 1;

MLreal64 gain = -12;
msg[2].param = ML_AUDIO_GAINS_REAL64_ARRAY;
msg[2].value.pReal64 = &gain
msg[2].length = 1;

msg[3].param = ML_AUDIO_SAMPLE_RATE_REAL64;
msg[3].value.real64 = 44100.0;

msg[4].param = ML_END;
mlSetControls(openPath, msg);        /* synchrone Message */

/* 4. Ausgabedatenstrom anweisen */

msg[0].param = ML_AUDIO_BUFFER_POINTER;
msg[0].value.pByte = audioBuffer;
msg[0].length = sizeof(audioBuffer);
msg[1].param = ML_AUDIO_UST_INT64;   /* Timestamp */
msg[2].param = ML_END;

mlSendBuffers(openPath, msg);        /* asynchrone Message: enqueue */

/* 5. Verarbeitung starten */

mlBeginTransfer(openPath);

/* ... */

/* 6. Auf Antwort warten */

MLint32 messageType;
mlReceiveMessage(openPath, &messageType, msg );
if (messageType == ML_BUFFERS_COMPLETE)
   printf("Buffer erfolgreich übertragen!\n");

/* 7. Pfad schliessen */

mlClose(openPath);
