Faun 0.2.5
A high-level C audio library
Loading...
Searching...
No Matches
faun.h
Go to the documentation of this file.
1#ifndef FAUN_H
2#define FAUN_H
3/* ___________
4 \_ _____/____ __ __ ____
5 | __) \__ \ | | \/ \
6 | \ / __ \| | / | \
7 \__ / (____ /____/|___| /
8 \/ \/ \/
9
10 Faun - A high-level C audio library
11
12 Copyright (c) 2022-2024 Karl Robillard
13 This code may be used under the terms of the MIT license (see faun.c).
14*/
15
16#include <stdint.h>
17#include <stdio.h>
18
19#define FAUN_VERSION_STR "0.2.5"
20#define FAUN_VERSION 0x000205
21
30
31enum FaunOpcode {
32 FO_END,
33 FO_WAIT, // 1/10 second units
34 FO_SOURCE, // Source #
35 FO_QUEUE, // Buffer #
36 FO_PLAY_BUF, // Buffer #, mode
37 FO_START_STREAM, // mode
38 FO_RESERVED0,
39 FO_SET_VOL, // Unit value
40 FO_SET_FADE, // 1/10 second units
41 FO_SET_END, // 1/10 second units
42 FO_LOOP_ON,
43 FO_LOOP_OFF,
44 FO_FADE_IN,
45 FO_FADE_OUT,
46 FO_VOL_LR, // L volume, R volume
47 FO_PAN, // L target, R target
48 FO_SIGNAL,
49 FO_CAPTURE,
50 /*
51 FO_SET_VOL_f, // float argN
52 FO_SET_FADE_f, // float argN
53 FO_VOL_LR_f, // float argN
54 FO_PAN_f, // float argN, float argN
55 */
56 FO_COUNT
57};
58
59#define FAUN_PROGRAM_MAX 64
60
61enum FaunFormat {
62 FAUN_FMT_S16 = 1,
63 FAUN_FMT_F32 = 2,
64 FAUN_FMT_MONO = 0,
65 FAUN_FMT_STEREO = 8,
66 FAUN_FMT_22050 = 0x10,
67 FAUN_FMT_44100 = 0x20
68};
69
79
80#define FAUN_PAIR(a,b) (((b+1) << 10) | a)
81#define FAUN_TRIO(a,b,c) (((c+1) << 20) | ((b+1) << 10) | a)
82#define FAUN_PID_SOURCE(pid) (pid & 0xff)
83
91
92typedef struct {
93 uint32_t id;
94 uint16_t signal;
95 uint16_t _pad;
96}
98
99#ifdef __cplusplus
100extern "C" {
101#endif
102
103const char* faun_startup(int bufferLimit, int sourceLimit,
104 int streamLimit, int progLimit, const char* appName);
105void faun_shutdown();
106void faun_suspend(int halt);
107void faun_setErrorStream(FILE*);
108int faun_pollSignals(FaunSignal* sigbuf, int count);
109void faun_waitSignal(FaunSignal* sigbuf);
110void faun_control(int si, int count, int command);
111void faun_setParameter(int si, int count, uint8_t param, float value);
112void faun_pan(int si, float finalVolL, float finalVolR, float period);
113void faun_program(int ei, const uint8_t* bytecode, int len);
114
115float faun_loadBuffer(int bi, const char* file, uint32_t offset, uint32_t size);
116float faun_loadBufferF(int bi, FILE* file, uint32_t size);
117float faun_loadBufferPcm(int bi, int format, const void* samples,
118 uint32_t frames);
119float faun_loadBufferSfx(int bi, const void* sfxParam);
120void faun_freeBuffers(int bi, int count);
121uint32_t faun_playSource(int si, int bi, int mode);
122uint32_t faun_playSourceVol(int si, int bi, int mode, float volL, float volR);
123
124uint32_t faun_playStream(int si, const char* file, uint32_t offset,
125 uint32_t size, int mode);
126void faun_playStreamPart(int si, double start, double duration, int mode);
127uint32_t faun_feedStream(int si, int (*func)(float*, int, void*), void* user);
128int faun_isPlaying(uint32_t pid);
129int faun_idleSource(int start, int end, int it);
130
131#ifdef __cplusplus
132}
133#endif
134
135#endif // FAUN_H
void faun_setParameter(int si, int count, uint8_t param, float value)
Set source or stream parameter.
Definition faun.c:2652
void faun_setErrorStream(FILE *)
Redirect error messages from stderr.
Definition faun.c:2560
uint32_t faun_playSourceVol(int si, int bi, int mode, float volL, float volR)
Begin playback of a buffer from a source and set channel volumes.
Definition faun.c:2981
void faun_waitSignal(FaunSignal *sigbuf)
Block calling thread until a signal is emitted.
Definition faun.c:2603
float faun_loadBufferPcm(int bi, int format, const void *samples, uint32_t frames)
Load PCM audio data from memory into a buffer.
Definition faun.c:2832
FaunParameter
Definition faun.h:84
@ FAUN_VOLUME
This is the volume (or fade in target) used when playback begins.
Definition faun.h:85
@ FAUN_FADE_PERIOD
Duration in seconds for fading in & out.
Definition faun.h:87
@ FAUN_END_TIME
Used to end playback of a source or stream before the buffer or stream file ends.
Definition faun.h:88
@ FAUN_VOLUME_APPLY
This sets the volume parameter like FAUN_VOLUME and also immediately changes the current volume.
Definition faun.h:86
uint32_t faun_playSource(int si, int bi, int mode)
Begin playback of a buffer from a source.
Definition faun.c:2946
int faun_isPlaying(uint32_t pid)
Check if a source or stream is still playing.
Definition faun.c:3138
uint32_t faun_playStream(int si, const char *file, uint32_t offset, uint32_t size, int mode)
Open a file and optionally begin streaming.
Definition faun.c:3023
uint32_t faun_feedStream(int si, int(*func)(float *, int, void *), void *user)
Begin streaming user generated samples via a callback function.
Definition faun.c:3102
float faun_loadBufferF(int bi, FILE *file, uint32_t size)
Load audio data from FILE into a PCM buffer.
Definition faun.c:2800
void faun_shutdown()
Called once when the program exits.
Definition faun.c:2510
float faun_loadBuffer(int bi, const char *file, uint32_t offset, uint32_t size)
Load a file into a PCM buffer.
Definition faun.c:2761
void faun_control(int si, int count, int command)
Send a single command to sources or streams.
Definition faun.c:2620
FaunCommand
Definition faun.h:22
@ FC_STOP
Halt playback and deactivate source.
Definition faun.h:24
@ FC_FADE_OUT
Fade volume to zero from the current play position over the FAUN_FADE_PERIOD.
Definition faun.h:27
@ FC_START
Start playing from the beginning of the source buffer or stream.
Definition faun.h:23
@ FC_RESUME
Continue playback from the point when FC_PAUSE was last used.
Definition faun.h:26
@ FC_PAUSE
Halt playback but keep source active to resume later.
Definition faun.h:25
void faun_pan(int si, float finalVolL, float finalVolR, float period)
Change volume of stereo channels over a period of time.
Definition faun.c:2674
void faun_freeBuffers(int bi, int count)
Free the memory used by a contiguous group of buffers.
Definition faun.c:2896
int faun_pollSignals(FaunSignal *sigbuf, int count)
Check for signals from sources and streams.
Definition faun.c:2574
FaunPlayMode
Definition faun.h:70
@ FAUN_PLAY_LOOP
Used to initiate playback of a source or stream and repeat it forever.
Definition faun.h:72
@ FAUN_SIGNAL_DONE
Used to generate a FaunSignal when the source or stream is finished playing.
Definition faun.h:75
@ FAUN_SIGNAL_PROG
The FaunSignal::signal identifier of a signal generated by the FO_SIGNAL program opcode.
Definition faun.h:76
@ FAUN_PLAY_FADE
Used to set both FAUN_PLAY_FADE_IN & FAUN_PLAY_FADE_OUT.
Definition faun.h:77
@ FAUN_PLAY_ONCE
Used to initiate playback of a source or stream a single time.
Definition faun.h:71
@ FAUN_PLAY_FADE_OUT
Decreases gain to 0.0 gradually just before the source or stream ends.
Definition faun.h:74
@ FAUN_PLAY_FADE_IN
Increase gain from 0.0 gradually when playing begins.
Definition faun.h:73
const char * faun_startup(int bufferLimit, int sourceLimit, int streamLimit, int progLimit, const char *appName)
Called once at program startup.
Definition faun.c:2392
void faun_playStreamPart(int si, double start, double duration, int mode)
Begin playing a segment from a stream.
Definition faun.c:3069
void faun_suspend(int halt)
Pause or resume mixing.
Definition faun.c:2550
int faun_idleSource(int start, int end, int it)
Find a source that is not playing.
Definition faun.c:3160
float faun_loadBufferSfx(int bi, const void *sfxParam)
Load audio data generated from SfxParams into a PCM buffer.
Definition faun.c:2876
void faun_program(int ei, const uint8_t *bytecode, int len)
Execute a Faun program.
Definition faun.c:2704
This struct is used for faun_pollSignals() & faun_waitSignal().
Definition faun.h:92
uint16_t signal
This is the FaunPlayMode event (FAUN_SIGNAL_DONE, FAUN_SIGNAL_PROG) which occurred.
Definition faun.h:94
uint32_t id
This is the playback identifier of the source generating the signal.
Definition faun.h:93