Hi
I'm poking around with NIM to see if I can do some audio stuff - basic electric guitar tuner on command-line but with a bit more flexibility.
To this end I'm poking around with nim-miniaudio to see if it can do realtime audio. This looks like the library to use from looking at the binding file. I'll need to set the device/samplerate/window.
I don't mind learning this as I need FFT too which looks like a similar futhark pattern.
First question - nimble install doesn't work for this lib - I read that it uses futhark for binding. Using choosenim on fedora. nim 2.2.6,nimble 0.22.2
I have cloned the nim-miniaudio repo into a subfolder and fetched the submodule, see below.
app.nimble
app.nim
/nim-miniaudio
But in my mind I'm unsure how to get this library building as part of my app.nim. Can I call /nim-miniaudio nimble file from app.nimble? I tried nimble build but that complains about having nothing to build. Do I need to install/build futhark?
task buildSubproject, "Build the sub":
echo "Building nim-miniaudio..."
exec "cd nim-miniaudio && nimble build -y"
Second question - is there nim installable library? I think nim-miniaudio is used for games.
Appreciate any help.
As far as I understand, the library you mentioned hasn’t been added to the Nimble package database. In that case, the best thing you can do is download the library and include it directly in your project. How can you do that? From what I see in the repo, you can rename the src folder to miniaudio_nim and place it, along with the miniaudio folder next to it, under your project’s own src directory. Then, in your main Nim file, just write import miniaudio_nim/miniaudio and you can start using the module.
Even though most of us tend to share our Nim projects as Nimble packages (some even publish them to different package managers), not every project follows that approach. But don’t worry, this way isn’t really difficult either.
Futhark doesn’t really concern you at this stage, it’s a tool used to automatically generate Nim wrappers for C libraries, and it was used here to create the Nim wrapper for miniaudio. You’ll simply be using it in your Nim project.
Note that a library doesn't have to be in the package database for nimble to download it for use by your project. Simply add the library's full url to the requires list in your project's .nimble file. For example, in myProject.nimble add the line
requires "https://github.com/SpotlightKid/nim-miniaudio"Hi, nim-miniaudio co-author here. I made a mistake when I overhauled this package started by beef331 when I moved the miniaudio sources into a submodule outside the "src" folder. nimble doesn't handle that properly.
So the package is currently broken, sorry.
I originally wanted to use it for a personal project but then decided to JACK directly instead, so this never got properly tested as a nimble package.
Try jill! (shameless plug)
Easy interface to jack, the usual linux go-to sound server. Based on @SpotlightKid's jack library.
thanks for clearing forest
I still use jack in reaper through pipewire - latency is okay through reaper. I'll kick tyres.
@cmc - I was able to install and get the code below compiling.
import jill, os
withJack input=(left), output=(), clientName = "Tuner", mainApp = false:
for i in 0..left.len:
echo left[i]
while true:
sleep(high(int))
The Tuner client appears in qpwgraph and I can manually connect to my mixer after running the app.
I'd prefer to connect from the app but appreciate this is on your roadmap.
The echo left[i] is giving me 0 when I strum - I need to get the frequencies for each chunk of 1024.
I will need to do some maths to isolate the note using FFT or something similar but for now I just need to echo out the frequency in chunks of 1024 samples.
Any ideas?
Nel
Sure! What you want is bitstream autocorrelation. That's actually on my roadmap for syn, a synth toolkit.
The best algorithm I've found so far is Tartini, based on the 2005 Paper by Philip McLeod, a smarter way to find pitch. I used it with SuperCollider and was going to vibe-port it to Nim, but didn't get around to yet.
Feel free to give it a whirl, if you like. You would check out this repo https://github.com/supercollider/sc3-plugins/ and instruct a coding agent to extract Tartini and turn it into a Nim procedure that works with jill.
Or you can wait for me to do it!
I can't even get the frequency at the moment. does your lib work with pipewire?
I'll have a go at the sc3 thing with claude but my experience so far is it's really bad at writing nim.
Yeah- what you get is the raw audio stream, getting the frequency is the purpose of the autocorrelation.
I've had just the opposite experience using the Opus 4.5 and 4.6 models. It started getting things right more often than not in my experience on Opus 4.0.
for single notes / frequency detection, Goertzel filters may be useful
Three versions, don't remember how well they work, so you may have to fiddle with it a bit:
https://gist.github.com/ingoogni/e1083ba50073c96f360357f2f1dbae62