#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#define M_PI_M2f (float)(M_PI+M_PI)
#define DEFAULT_RATE 44100
#define DEFAULT_CHANNELS 2
#define DEFAULT_VOLUME 0.7f
#define BUFFER_SIZE (16*1024)
#define MIN_SIZE 256
#define MAX_SIZE BUFFER_SIZE
static float samples[BUFFER_SIZE * DEFAULT_CHANNELS];
struct data {
int eventfd;
bool running;
float accumulator;
float buffer[BUFFER_SIZE * DEFAULT_CHANNELS];
};
static void fill_f32(struct data *d, float *samples, int n_frames)
{
float val;
int i, c;
for (i = 0; i < n_frames; i++) {
d->accumulator += M_PI_M2f * 440 / DEFAULT_RATE;
if (d->accumulator >= M_PI_M2f)
d->accumulator -= M_PI_M2f;
val = sinf(d->accumulator) * DEFAULT_VOLUME;
for (c = 0; c < DEFAULT_CHANNELS; c++)
samples[i * DEFAULT_CHANNELS + c] = val;
}
}
static void push_samples(void *userdata, float *samples, uint32_t n_samples)
{
struct data *data = userdata;
int32_t filled;
uint32_t index, avail, stride = sizeof(float) * DEFAULT_CHANNELS;
uint64_t count;
float *s = samples;
while (n_samples > 0) {
while (true) {
avail = BUFFER_SIZE - filled;
if (avail > 0)
break;
}
if (avail > n_samples)
avail = n_samples;
data->buffer, BUFFER_SIZE * stride,
(index % BUFFER_SIZE) * stride,
s, avail * stride);
s += avail * DEFAULT_CHANNELS;
n_samples -= avail;
}
}
static void on_process(void *userdata)
{
struct data *data = userdata;
uint8_t *p;
uint32_t index, to_read, to_silence;
int32_t avail, n_frames, stride;
return;
}
return;
stride = sizeof(float) * DEFAULT_CHANNELS;
to_read = avail > 0 ?
SPA_MIN(avail, n_frames) : 0;
to_silence = n_frames - to_read;
if (to_read > 0) {
data->buffer, BUFFER_SIZE * stride,
(index % BUFFER_SIZE) * stride,
p, to_read * stride);
}
if (to_silence > 0)
memset(
SPA_PTROFF(p, to_read * stride,
void), 0, to_silence * stride);
}
.process = on_process,
};
static void do_quit(void *userdata, int signal_number)
{
struct data *data = userdata;
data->running = false;
}
int main(int argc, char *argv[])
{
struct data data = { 0, };
NULL);
if (argc > 1)
"audio-src-ring",
props,
&stream_events,
.channels = DEFAULT_CHANNELS,
.rate = DEFAULT_RATE ));
params, 1);
fill_f32(&
data, samples, BUFFER_SIZE);
push_samples(&
data, samples, BUFFER_SIZE);
srand(time(NULL));
uint32_t
size = rand() % ((MAX_SIZE - MIN_SIZE + 1) + MIN_SIZE);
}
return 0;
}