Open tribusonz-2 opened 3 months ago
This is an example implementation in C.
/* Lexical Environment */
typedef struct {
long pos;
long len;
} snd_env;
/* Queries the pointer position */
long
ptr_idx(snd_env *env)
{
if (env->pos >= env->len) return -1; // Notify '-1' if the number of arrays more
return env->pos++;
}
/* Closure */
typedef struct {
snd_env env;;
long (*lambda)(snd_env *);
} snd_closure;
/* Instance */
snd_closure snd_broker(long len)
{
snd_closure c;
c.env.pos = 0;
c.env.len = len;
c.lambda = ptr_idx;
return c;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int
main(void)
{
size_t size = 16;
snd_closure c1 = snd_broker((long)size);
double *s = (double *)malloc(size * sizeof(double));
for (size_t i = 0; i < size; i++)
{
s[i] = 0.1 * sin(2 * M_PI * 500.0 * i / 8000);
}
for (long i = 0; i < 20; i++)
{
const long index = c1.lambda(&c1.env);
const double snd = index == -1 ? 0.0 : s[index];
printf("%02ld % f\n", i, snd);
}
free(s);
}
/* Result
00 0.000000
01 0.038268
02 0.070711
03 0.092388
04 0.100000
05 0.092388
06 0.070711
07 0.038268
08 0.000000
09 -0.038268
10 -0.070711
11 -0.092388
12 -0.100000
13 -0.092388
14 -0.070711
15 -0.038268
16 0.000000
17 0.000000
18 0.000000
19 0.000000
*/
I thought Mutex was not necessary. The functions of these closures will be aggregated into procedures. Since the procedure is for each chunk, a critical section will always occur. From the perspective of atomicity, it seems like any amount is necessary.
schedule(io) {
threads << Thread(each_chunk) {
procedure(methods) {
Mutex(lock) { read/write }
}
}
}
When inputting and outputting audio files, data exchange using closures is most suitable.
I get a slightly meaningful error when writing with the #write method.
Passing PCM classes with different frequencies/sizes in an array throws a semantic error similar to the following:
"Exporting each channel's the different ### is not supported yet"
Yet... What "yet"?
SLAKE (Takehiko Fujii) told me "to create a sampler", so I created a routine like the one below using closures.
Have you ever noticed that an array of audio data keeps creating 0.0 forever even when it reaches the end? Yes, just write this in C.