Announce: scnim - Write SuperCollider UGens in Nim (Proof of Concept)

carlo-3+kk896uEWp9MqFnZXqmrwC/[email protected]
Newsgroups gmane.comp.audio.supercollider.devel
Message-ID <[email protected]>
Hi everyone!

I was toying with the thought of trying the Nim programming language- it
feels a bit like Python or Ruby to work with, which is great for an old
scripter like me, but is compiled to C++ and is just as fast. It also has
very powerful macros, which allows for a lot of work to be done before
compiling- this somewhat resolves the tension between clean code and fast
code, a perfect match for a realtime system.

So I decided to give it a whirl by writing a SuperCollider UGen in it, and
it works really well! Here's what a basic UGen looks like in Nim:


---------
# ExampleUGens.nim
import
  PlugIn,InterfaceTable,Unit,ptrmath
  
proc api_version(): int {.cdecl,exportc,dynlib.} =
  return sc_api_version;

proc server_type(): ServerType {.cdecl,exportc,dynlib.}=
  return when defined(SUPERNOVA): sc_server_supernova else:
sc_server_scsynth

var ft: ptr InterfaceTable

type
  Example = object of Unit

proc next(unit: ptr Example, numSamples: cint) {.cdecl,exportc,dynlib.} =
  var
    input = unit.mInBuf[]
    output = unit.mOutBuf[]

  for i in 0..numSamples:
    output[i] = input[i]

proc ctor(unit: ptr Example) {.cdecl,exportc,dynlib.} =
  unit.mCalcFunc = cast[UnitCalcFunc](next)

proc load(inTable: ptr InterfaceTable) {.cdecl,exportc,dynlib.} =
  ft = inTable;
  discard ft.fDefineUnit("Example", sizeof(Example),
cast[UnitCtorFunc](ctor), nil, 0'u32)
---------


This is still proof-of-concept and fairly gritty and low-level by Nim
standards, but it's already far cry from the cognitive overhead when writing
C++ UGens directly, especially if you just look at the "next" procedure- the
rest is boilerplate and yet needs to be hidden away. It's also missing
features.

If you feel like playing with this, here is the repository with some more
details and a runnable example:

https://github.com/carlocapocasa/scnim

Have a great time!

Carlo




--
Sent from: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/SuperCollider-Developers-New-Use-this-f2681767.html

_______________________________________________
sc-dev mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-dev/
search: http://www.listarc.bham.ac.uk/lists/sc-dev/search/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.