I've been trying to write a simple program to play a sine wave for specific 
duration. I tried to use [http://libsound.io](http://libsound.io) and a Nim 
wrapper for it: [https://github.com/ul/soundio](https://github.com/ul/soundio).

I wrote the program in C first, which can be found here: 
[https://gitlab.com/snippets/1883765](https://gitlab.com/snippets/1883765). 
This is closely based off the following example provided with libsoundio: 
[http://libsound.io/doc-2.0.0/sio_sine_8c-example.html](http://libsound.io/doc-2.0.0/sio_sine_8c-example.html).

Next I tried to write it in Nim, which can be found here: 
[https://gitlab.com/snippets/1883764](https://gitlab.com/snippets/1883764). I 
tried to make it as similar as possible to the C version. However, this crashed 
unpredictably for me. The crash almost certainly happens in `write_callback` or 
`write_sample`. My guess is that the crash is due to some issue in lines 73 - 
94, where there is some pointer arithmetic.

A command line parameter which specifies the delay during the main loop is 
provided. Setting it to higher values sometimes allows the program to run 
without crashing.

**Build instructions**
    
    
    nimble install soundio
    nim c beep.nim
    
    
     Run

**Usage**
    
    
    ./beep 1000 # 1000 milliseconds of delay (generally runs without crashing 
for me)
    ./beep 10 # 10 milliseconds of delay (almost always crashes before the beep 
finishes)
    ./beep # 0 milliseconds of delay (almost always crashes before the beep 
finishes)
    
    
     Run

**My system info**

  * Nim Compiler Version 0.20.0 [Linux: amd64]
  * libsoundio version: 1.0.2-1
  * Ubuntu 18.04.1 LTS x86_644



**TL;DR** This code crashes, somewhere in `write_callback`: 
[https://gitlab.com/snippets/1883764](https://gitlab.com/snippets/1883764)

Thanks for the help!

Reply via email to