Example
This is a brief example to demonstrate how Quiet works. Full examples can be found for interfacing with your soundcard and with .WAV files in the repo on Github.
Transmitting
We'll start by writing the message "Hello, World!" into soundwaves. We'll choose an audible frequency so that it will be easy to confirm that something is being transmitted.
#include <string.h> #include <unistd.h> #include "quiet-portaudio.h" int main() { /* start up PortAudio, which will interface quiet to the soundcard */ Pa_Initialize(); /* get a profile and set up encoder options, which controls Quiet's modem */ quiet_encoder_options *encodeOpt = quiet_encoder_profile_filename("/usr/local/share/quiet/quiet-profiles.json", "audible"); /* get some default options about device and soundcard setup from PortAudio */ PaDeviceIndex device = Pa_GetDefaultOutputDevice(); const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(device); double sampleRate = deviceInfo->defaultSampleRate; PaTime latency = deviceInfo->defaultLowOutputLatency; /* select a power of 2 sample buffer size to interface soundcard with * this should be between 128 and 16384 */ const size_t sampleBufferSize = (1 << 14); /* start the quiet/PortAudio interface */ quiet_portaudio_encoder *encoder = quiet_portaudio_encoder_create(encodeOpt, device, latency, sampleRate, sampleBufferSize); /* create our message in a uint8_t array */ const uint8_t msg[] = "Hello, World!"; /* send the message. this just writes to a send queue * behind the scenes, quiet will encode and send as sound samples */ quiet_portaudio_encoder_send(encoder, msg, sizeof(msg)); /* stop the quiet/PortAudio interface */ quiet_portaudio_encoder_close(encoder); quiet_portaudio_encoder_destroy(encoder); /* free encoder options we created earlier */ free(encodeOpt); /* shut down PortAudio */ Pa_Terminate(); return 0; }
Now compile and run the resulting program. If this example is saved as encoder.c
then
clang encoder.c -o encoder -lquiet -ljansson -lportaudio -lliquid -lfec
The resulting program will be compiled as encoder
. Running with ./encoder
should emit a short audible tone and then quit.
Receiving
#include <string.h> #include <unistd.h> #include "quiet-portaudio.h" int main() { /* start up PortAudio, which will interface quiet to the soundcard */ Pa_Initialize(); /* get a profile and set up decoder options, which controls Quiet's modem */ quiet_decoder_options *decodeOpt = quiet_decoder_profile_filename("/usr/local/share/quiet/quiet-profiles.json", "audible"); /* get some default options about device and soundcard setup from PortAudio */ PaDeviceIndex device = Pa_GetDefaultInputDevice(); const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(device); double sampleRate = deviceInfo->defaultSampleRate; PaTime latency = deviceInfo->defaultLowInputLatency; /* select a power of 2 sample buffer size to interface soundcard with * this should be between 128 and 16384 */ const size_t sampleBufferSize = (1 << 14); /* start the quiet/PortAudio interface */ quiet_portaudio_decoder *decoder = quiet_portaudio_decoder_create(decodeOpt, device, latency, sampleRate, sampleBufferSize); /* recover message of up to 1024 bytes */ const size_t writeBufferSize = 1024; uint8_t *writeBuffer = malloc(writeBufferSize); while (true) { /* pump some samples from soundcard into quiet */ quiet_portaudio_decoder_consume(decoder); /* check quiet's receive buffer */ ssize_t read = quiet_portaudio_decoder_recv(decoder, writeBuffer, writeBufferSize); if (read < 0) { continue; } /* read succeeded, so we have a message. print it out. */ printf("%.*s\n", read, writeBuffer); /* we are done */ break; } /* free message buffer and quiet/PortAudio interface */ free(writeBuffer); quiet_portaudio_decoder_destroy(decoder); /* free decoder options we created earlier */ free(decodeOpt); /* shut down PortAudio */ Pa_Terminate(); return 0; }
Now compile and run. If this example is saved as decoder.c
then
clang decoder.c -o decoder -lquiet -ljansson -lportaudio -lliquid -lfec
should do the trick.
End-to-end
Now that both are compiled, start the decoder in the background first
./decoder &
and then the encoder
./encoder
You should hear an audible tone and see the message "Hello, World!" printed on your console. Both programs will then exit.