Waveform analysis/Top and tail: Difference between revisions

Added Wren
(Added Wren)
Line 393:
system("aplay notes.wav")
end if</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>/* waveform_analysis_top_and_tail.wren */
 
class C {
foreign static getInput(maxSize)
 
foreign static sox(args)
 
foreign static removeFile(name)
}
 
var sec = "00:00:01"
 
var name = ""
while (name == "") {
System.write("Enter name of audio file to be trimmed : ")
name = C.getInput(80)
}
 
var name2 = ""
while (name2 == "") {
System.write("Enter name of output file : ")
name2 = C.getInput(80)
}
 
var squelch = 0
while (!squelch || squelch < 1 || squelch > 10) {
System.write("Enter squelch level \% max (1 to 10) : ")
squelch = Num.fromString(C.getInput(5))
}
var squelchS = squelch.toString + "\%"
 
var tmp1 = "tmp1_" + name
var tmp2 = "tmp2_" + name
 
// Trim audio below squelch level from start and output to tmp1.
var args = [name, tmp1, "silence", "1", sec, squelchS]
C.sox(args.join(" "))
 
// Reverse tmp1 to tmp2.
args = [tmp1, tmp2, "reverse"]
C.sox(args.join(" "))
 
// Trim audio below squelch level from tmp2 and output to tmp1.
args = [tmp2, tmp1, "silence", "1", sec, squelchS]
C.sox(args.join(" "))
 
// Reverse tmp1 to the output file.
args = [tmp1, name2, "reverse"]
C.sox(args.join(" "))
 
// Remove the temporary files.
C.removeFile(tmp1)
C.removeFile(tmp2)</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_sox(WrenVM* vm) {
const char *args = wrenGetSlotString(vm, 1);
char command[strlen(args) + 4];
strcpy(command, "sox ");
strcat(command, args);
system(command);
}
 
void C_removeFile(WrenVM* vm) {
const char *name = wrenGetSlotString(vm, 1);
if (remove(name) != 0) perror("Error deleting file.");
}
 
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, "sox(_)") == 0) return C_sox;
if (isStatic && strcmp(signature, "removeFile(_)") == 0) return C_removeFile;
}
}
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 = "waveform_analysis_top_and_tail.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|AWK}}
9,482

edits