Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
rapid/audio fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidev committed May 5, 2020
1 parent e51d4d0 commit 60134a9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
26 changes: 15 additions & 11 deletions src/rapid/audio/device.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ type
fUnderruns: int
RAudioDevice* = ref RAudioDeviceObj

proc teardownDevice(device: ref RAudioDeviceObj) =
soundio_outstream_destroy(device.ostream)
soundio_device_unref(device.device)
soundio_destroy(device.sio)

proc create(dev: var RAudioDevice) =
new(dev, teardownDevice)

proc limit(val: float): float =
result = clamp(val, -1.0, 1.0)

Expand Down Expand Up @@ -93,16 +101,11 @@ proc errorCallback(outstream: ptr SoundIoOutStream, errcode: cint) {.cdecl.} =
if errcode.SoundIoError != SoundIoErrorUnderflow:
raise newException(AudioError, $soundio_strerror(errcode))

proc teardownDevice(device: ref RAudioDeviceObj) =
soundio_outstream_destroy(device.ostream)
soundio_device_unref(device.device)
soundio_destroy(device.sio)

proc newRAudioDevice*(name = "rapid/audio device",
latency = 0.1): RAudioDevice =
## Creates a new audio device, with the specified name.
var error: cint
new(result, teardownDevice)
result.create()

result.sio = soundio_create()
result.sio.app_name = name
Expand All @@ -122,11 +125,6 @@ proc newRAudioDevice*(name = "rapid/audio device",
else: SoundIoFormatS16BE
outstream.sample_rate = ROutputSampleRate
outstream.software_latency = latency
outstream.name = "rapid/audio"
outstream.userdata = cast[pointer](result)
outstream.write_callback = writeCallback
outstream.underflow_callback = underflowCallback
outstream.error_callback = errorCallback
result.ostream = outstream

if (error = soundio_outstream_open(outstream); error != 0):
Expand All @@ -138,6 +136,12 @@ proc newRAudioDevice*(name = "rapid/audio device",
"Could not set channel layout: " &
$soundio_strerror(outstream.layout_error))

outstream.name = "rapid/audio"
outstream.userdata = cast[pointer](result)
outstream.write_callback = writeCallback
outstream.underflow_callback = underflowCallback
outstream.error_callback = errorCallback

proc attach*(device: RAudioDevice, sampler: RSampler) =
## Attaches a sampler to the device. This must be done before starting audio
## playback, otherwise a buffer underflow will occur (because there's nothing
Expand Down
4 changes: 4 additions & 0 deletions src/rapid/gfx/rcolor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ proc gray*(gray: ColorCh, a = 255.ColorCh): RColor =
proc hex*(str: string): RColor =
## Parses a hex code into a color.
## Possible combinations are RGB, RGBA, RRGGBB, or RRGGBBAA.
## An extra # is allowed at the beginning.
## Anything else triggers an assertion.
var str = str
if str.len > 0 and str[0] == '#':
str = str[1..^1]
assert str.len in [3, 4, 6, 8]
var
r, g, b: int
Expand Down
10 changes: 9 additions & 1 deletion src/rapid/lib/soundio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const

static:
gitPull("https://github.com/andrewrk/libsoundio", BaseDir,
"src/*\nsoundio/*\n")
"src/*\nsoundio/*\n", checkout = "2.0.0")

cIncludeDir(BaseDir)
cIncludeDir(Incl)
Expand Down Expand Up @@ -127,6 +127,14 @@ static:
when defined(RCompileDebug):
echo configH

cOverride:
type
SoundioBool = bool

cPlugin:
proc onSymbol(sym: var Symbol) {.exportc, dynlib.} =
if sym.name == "_Bool": sym.name = "SoundioBool"

cImport(Incl/"soundio.h")

cDefine("SOUNDIO_STATIC_LIBRARY")
Expand Down

0 comments on commit 60134a9

Please sign in to comment.