Task: Audio frequency generator

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_frequency_generator.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;
}




Go: package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
    "os/exec"
    "strconv"
)

func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    freq := 0
    for freq < 40 || freq > 10000 {
        fmt.Print("Enter frequency in Hz (40 to 10000) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        freq, _ = strconv.Atoi(input)
    }
    freqS := strconv.Itoa(freq)

    vol := 0
    for vol < 1 || vol > 50 {
        fmt.Print("Enter volume in dB (1 to 50) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        vol, _ = strconv.Atoi(input)
    }
    volS := strconv.Itoa(vol)

    dur := 0.0
    for dur < 2 || dur > 10 {
        fmt.Print("Enter duration in seconds (2 to 10) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        dur, _ = strconv.ParseFloat(input, 64)
    }
    durS := strconv.FormatFloat(dur, 'f', -1, 64)

    kind := 0
    for kind < 1 || kind > 3 {
        fmt.Print("Enter kind (1 = Sine, 2 = Square, 3 = Sawtooth) : ")
        scanner.Scan()
        input := scanner.Text()
        check(scanner.Err())
        kind, _ = strconv.Atoi(input)
    }
    kindS := "sine"
    if kind == 2 {
        kindS = "square"
    } else if kind == 3 {
        kindS = "sawtooth"
    }

    args := []string{"-n", "synth", durS, kindS, freqS, "vol", volS, "dB"}
    cmd := exec.Command("play", args...)
    err := cmd.Run()
    check(err)
}