Audio overlap loop: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 367: | Line 367: | ||
make_gui |
make_gui |
||
</lang> |
</lang> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
The ability to call external processes such as ''SoX'' is expected to be added to Wren-cli in the next release. In the meantime, we embed the following Wren script in a C host to complete this task. |
|||
<lang ecmascript>/* audio_overlap_loop.wren */ |
|||
class C { |
|||
foreign static getInput(maxSize) |
|||
foreign static play(args) |
|||
} |
|||
var fileName = "loop.wav" |
|||
var reps = 0 |
|||
while (!reps || !reps.isInteger || reps < 1 || reps > 6) { |
|||
System.write("Enter number of repetitions (1 to 6) : ") |
|||
reps = Num.fromString(C.getInput(1)) |
|||
} |
|||
var delay = 0 |
|||
while (!delay || !delay.isInteger || delay < 50 || delay > 500) { |
|||
System.write("Enter delay between repetitions in microseconds (50 to 500) : ") |
|||
delay = Num.fromString(C.getInput(3)) |
|||
} |
|||
var decay = 0 |
|||
while (!decay || decay < 0.2 || decay > 0.9) { |
|||
System.write("Enter decay between repetitions (0.2 to 0.9) : ") |
|||
decay = Num.fromString(C.getInput(5)) |
|||
} |
|||
var args = [fileName, "-V1", "echo", "0.8", "0.7"] |
|||
var decay2 = 1 |
|||
for (i in 1..reps) { |
|||
var delayS = (i * delay).toString |
|||
decay2 = decay2 * decay |
|||
var decayS = decay2.toString |
|||
args.add(delayS) |
|||
args.add(decayS) |
|||
} |
|||
C.play(args.join(" "))</lang> |
|||
<br> |
|||
We now embed this in the following C program, compile and run it. |
|||
<lang c>#include <stdio.h> |
|||
#include <stdio_ext.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include "wren.h" |
|||
void C_getInput(WrenVM* vm) { |
|||
int maxSize = (int)wrenGetSlotDouble(vm, 1) + 2; |
|||
char input[maxSize]; |
|||
fgets(input, maxSize, stdin); |
|||
__fpurge(stdin); |
|||
input[strcspn(input, "\n")] = 0; |
|||
wrenSetSlotString(vm, 0, (const char*)input); |
|||
} |
|||
void C_play(WrenVM* vm) { |
|||
const char *args = wrenGetSlotString(vm, 1); |
|||
char command[strlen(args) + 5]; |
|||
strcpy(command, "play "); |
|||
strcat(command, args); |
|||
system(command); |
|||
} |
|||
WrenForeignMethodFn bindForeignMethod( |
|||
WrenVM* vm, |
|||
const char* module, |
|||
const char* className, |
|||
bool isStatic, |
|||
const char* signature) { |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "C") == 0) { |
|||
if (isStatic && strcmp(signature, "getInput(_)") == 0) return C_getInput; |
|||
if (isStatic && strcmp(signature, "play(_)") == 0) return C_play; |
|||
} |
|||
} |
|||
return NULL; |
|||
} |
|||
static void writeFn(WrenVM* vm, const char* text) { |
|||
printf("%s", text); |
|||
} |
|||
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) { |
|||
switch (errorType) { |
|||
case WREN_ERROR_COMPILE: |
|||
printf("[%s line %d] [Error] %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_STACK_TRACE: |
|||
printf("[%s line %d] in %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_RUNTIME: |
|||
printf("[Runtime Error] %s\n", msg); |
|||
break; |
|||
} |
|||
} |
|||
char *readFile(const char *fileName) { |
|||
FILE *f = fopen(fileName, "r"); |
|||
fseek(f, 0, SEEK_END); |
|||
long fsize = ftell(f); |
|||
rewind(f); |
|||
char *script = malloc(fsize + 1); |
|||
fread(script, 1, fsize, f); |
|||
fclose(f); |
|||
script[fsize] = 0; |
|||
return script; |
|||
} |
|||
int main(int argc, char **argv) { |
|||
WrenConfiguration config; |
|||
wrenInitConfiguration(&config); |
|||
config.writeFn = &writeFn; |
|||
config.errorFn = &errorFn; |
|||
config.bindForeignMethodFn = &bindForeignMethod; |
|||
WrenVM* vm = wrenNewVM(&config); |
|||
const char* module = "main"; |
|||
const char* fileName = "audio_overlap_loop.wren"; |
|||
char *script = readFile(fileName); |
|||
WrenInterpretResult result = wrenInterpret(vm, module, script); |
|||
switch (result) { |
|||
case WREN_RESULT_COMPILE_ERROR: |
|||
printf("Compile Error!\n"); |
|||
break; |
|||
case WREN_RESULT_RUNTIME_ERROR: |
|||
printf("Runtime Error!\n"); |
|||
break; |
|||
case WREN_RESULT_SUCCESS: |
|||
break; |
|||
} |
|||
wrenFreeVM(vm); |
|||
free(script); |
|||
return 0; |
|||
}</lang> |
|||
{{omit from|Bc}} |
{{omit from|Bc}} |