Shirakumo / cl-out123

Bindings to libout123, allowing cross-platform audio output.
https://shirakumo.github.io/cl-out123
zlib License
4 stars 1 forks source link

Arithmetic error DIVISION-BY-ZERO signalled on OSX #1

Open svetlyak40wt opened 4 years ago

svetlyak40wt commented 4 years ago

I've tried today to reproduce an example from: https://github.com/Shirakumo/cl-flac/blob/master/test.lisp, but cl-out123:connect does not work as expected.

This minimal code throws an error:

CL-USER> (ql:quickload :cl-out123)
To load "cl-out123":
  Load 1 ASDF system:
    cl-out123
; Loading "cl-out123"
..
(:CL-OUT123)
CL-USER> (cl-out123:make-output NIL)
#<CL-OUT123:OUTPUT "SBCL COMMON-LISP-USER">
CL-USER> (cl-out123:connect *)
; Debugger entered on #<DIVISION-BY-ZERO {100342B373}>
arithmetic error DIVISION-BY-ZERO signalled
   [Condition of type DIVISION-BY-ZERO]

Restarts:
 0: [RETRY] Retry SLY mREPL evaluation request.
 1: [*ABORT] Return to SLY's top level.
 2: [ABORT] abort thread (#<THREAD "sly-channel-1-mrepl-remote-1" RUNNING {1003935B33}>)

Backtrace:
 0: ("bogus stack frame")
 1: ("foreign function: _ZN17SincKernelFactory19ReferenceSincKernelEiidd")
 2: ("foreign function: _ZN10Resampler2C2Eddiiidb")
 3: ("foreign function: _ZN19SampleRateConverter16ReplaceResamplerEv")
 4: ("foreign function: _ZN19PCMConverterFactory6AddSRCER14StreamDescPairRK18ChainBuildSettingsRNSt3__16vectorIP22BufferedAudioConverterNS5_9allocatorIS8_EEEE")
 5: ("foreign function: _ZN19PCMConverterFactory19BuildConverterChainERK14StreamDescPairRK18ChainBuildSettingsP19AudioConverterChainPS_")
 6: ("foreign function: _AudioConverterNewInternal")
 7: ("foreign function: DefaultOutputAUFactory")
 8: ("foreign function: DefaultOutputAUFactory")
 9: ("foreign function: DefaultOutputAUFactory")
10: ("foreign function: DefaultOutputAUFactory")
11: ("foreign function: DefaultOutputAUFactory")
12: ("foreign function: DefaultOutputAUFactory")
13: ((FLET SB-THREAD::WITH-MUTEX-THUNK :IN CL-OUT123:CONNECT))
14: ((FLET "WITHOUT-INTERRUPTS-BODY-1" :IN SB-THREAD::CALL-WITH-MUTEX))
15: (SB-THREAD::CALL-WITH-MUTEX #<CLOSURE (FLET SB-THREAD::WITH-MUTEX-THUNK :IN CL-OUT123:CONNECT) {4FFEC6B}> #<SB-THREAD:MUTEX "output lock" owner: #<SB-THREAD:THRE$
16: (CL-OUT123:CONNECT #<CL-OUT123:OUTPUT "SBCL COMMON-LISP-USER"> :DRIVER NIL :DEVICE NIL)
17: (SB-INT:SIMPLE-EVAL-IN-LEXENV (CL-OUT123:CONNECT *) #<NULL-LEXENV>)
18: (EVAL (CL-OUT123:CONNECT *))
19: ((LAMBDA NIL :IN SLYNK-MREPL::MREPL-EVAL-1))

Second attempt to call (cl-out123:connect *) just hangs.

Shinmera commented 4 years ago

Does it help if you wrap everything in (sb-int:with-float-traps-masked (:divide-by-zero :overflow :underflow :inexact :invalid) ...)

svetlyak40wt commented 4 years ago

Tried to use a version built by Homebrew, but without success, because cl-out123 inserts it's static directory in the path:

CL-USER> (ql:quickload :cffi)
(:CFFI)

CL-USER> (push "/usr/local/Cellar/mpg123/1.25.13/lib/" cffi:*foreign-library-directories*)
("/usr/local/Cellar/mpg123/1.25.13/lib/"
 (CFFI::EXPLODE-PATH-ENVIRONMENT-VARIABLE "LD_LIBRARY_PATH")
 (CFFI::EXPLODE-PATH-ENVIRONMENT-VARIABLE "DYLD_LIBRARY_PATH") (UIOP/OS:GETCWD)
 (CFFI::DARWIN-FALLBACK-LIBRARY-PATH))
CL-USER> (ql:quickload :cl-out123)
To load "cl-out123":
  Load 1 ASDF system:
    cl-out123
; Loading "cl-out123"

(:CL-OUT123)
CL-USER> (cl-out123:make-output NIL)
#<CL-OUT123:OUTPUT "SBCL COMMON-LISP-USER">
CL-USER> (cl-out123:connect *)
; Debugger entered on #<DIVISION-BY-ZERO {1003920EF3}>
[1] CL-USER> 
; Evaluation aborted on #<DIVISION-BY-ZERO {1003920EF3}>
CL-USER> cffi:*foreign-library-directories*
(#P"/Users/art/projects/lisp/lisp-project-of-the-day/.qlot/dists/ultralisp/software/Shirakumo-cl-out123-20190627104836/static/"
 "/usr/local/Cellar/mpg123/1.25.13/lib/"
 (CFFI::EXPLODE-PATH-ENVIRONMENT-VARIABLE "LD_LIBRARY_PATH")
 (CFFI::EXPLODE-PATH-ENVIRONMENT-VARIABLE "DYLD_LIBRARY_PATH") (UIOP/OS:GETCWD)
 (CFFI::DARWIN-FALLBACK-LIBRARY-PATH))
CL-USER> 
svetlyak40wt commented 4 years ago

Does it help if you wrap everything in (sb-int:with-float-traps-masked (:divide-by-zero :overflow :underflow :inexact :invalid) ...)

Yes, it helps.

But late, when I call play:

CL-USER> (let ((buffersize (* (cl-flac:channels *file*)
                              (/ (cl-flac:samplerate *file*) 100))))
           (cffi:with-foreign-object (buffer :float buffersize)
             (loop for read = (cl-flac:read-directly *file* buffer (* buffersize 4))
                   for played = (cl-out123:play #v1 buffer read)
                   while (< 0 read)
                   do (when (/= played read)
                        (format T "~&Warning: playback not catching up with input by ~a bytes."
                                (- read played))))))

it fails with this condition:

#<OUTPUT "SBCL COMMON-LISP-USER" coreaudio/NIL> failed to play back: no active audio device
Shinmera commented 4 years ago

Hmm, odd. I'll have to take a look some other time.

svetlyak40wt commented 4 years ago

BTW, I've patched cl-out123 to inject Homebrew path instead:

CL-USER> cffi:*foreign-library-directories*
(#P"/usr/local/Cellar/mpg123/1.25.13/lib/"
 (CFFI::EXPLODE-PATH-ENVIRONMENT-VARIABLE "LD_LIBRARY_PATH")
 (CFFI::EXPLODE-PATH-ENVIRONMENT-VARIABLE "DYLD_LIBRARY_PATH") (UIOP/OS:GETCWD)
 (CFFI::DARWIN-FALLBACK-LIBRARY-PATH))

But this did not help.