How to resolve the algorithm Record sound step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Record sound step by step in the Wren programming language
Table of Contents
Problem Statement
Record a monophonic 16-bit PCM sound into either memory space, a file or array. (This task neglects to specify the sample rate, and whether to use signed samples. The programs in this page might use signed 16-bit or unsigned 16-bit samples, at 8000 Hz, 44100 Hz, or any other sample rate. Therefore, these programs might not record sound in the same format.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Record sound step by step in the Wren programming language
Source code in the wren programming language
/* record_sound.wren */
class C {
foreign static getInput(maxSize)
foreign static arecord(args)
foreign static aplay(name)
}
var name = ""
while (name == "") {
System.write("Enter output file name (without extension) : ")
name = C.getInput(80)
}
name = name + ".wav"
var rate = 0
while (!rate || !rate.isInteger || rate < 2000 || rate > 192000) {
System.write("Enter sampling rate in Hz (2000 to 192000) : ")
rate = Num.fromString(C.getInput(6))
}
var rateS = rate.toString
var dur = 0
while (!dur || dur < 5 || dur > 30) {
System.write("Enter duration in seconds (5 to 30) : ")
dur = Num.fromString(C.getInput(5))
}
var durS = dur.toString
System.print("\nOK, start speaking now...")
// Default arguments: -c 1, -t wav. Note only signed 16 bit format supported.
var args = ["-r", rateS, "-f", "S16_LE", "-d", durS, name]
C.arecord(args.join(" "))
System.print("\n'%(name)' created on disk and will now be played back...")
C.aplay(name)
System.print("\nPlay-back completed.")
#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_arecord(WrenVM* vm) {
const char *args = wrenGetSlotString(vm, 1);
char command[strlen(args) + 8];
strcpy(command, "arecord ");
strcat(command, args);
system(command);
}
void C_aplay(WrenVM* vm) {
const char *name = wrenGetSlotString(vm, 1);
char command[strlen(name) + 6];
strcpy(command, "aplay ");
strcat(command, name);
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, "arecord(_)") == 0) return C_arecord;
if (isStatic && strcmp(signature, "aplay(_)") == 0) return C_aplay;
}
}
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 = "record_sound.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;
}
You may also check:How to resolve the algorithm Variable size/Set step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the Nim programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Shen programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Python programming language