What is a simple way to play a .wav file in Nim on OSX?

冷暖自知 提交于 2020-01-24 19:16:20

问题


I am trying to play a wav file in a very simple program that looks like this, currently attempting to use nim-csfml:

import csfml_audio

var alarmsong = newMusic("alarm.wav")
alarmsong.play()

but it appears to be relying on the existence of libcsfml.audio, and while my program compiles just fine, when I try to actually run it I get an error

| => ./alarm
could not load: libcsfml-audio.so 

(I have a libcsfml-audio.dylib instead, being that I used the OSX shared libraries for csfml/sfml)

Is there some other way to play a .wav file in Nim?


Edit 1:

After the PR made by @def-, I now get a different, slightly more comforting error, which is probably due to some poor understanding of how nim deals with shared libraries:

| => ./alarm
could not load: libcsfml-audio.dylib

I added path = "/usr/local/lib" to my nim.cfg file, but it didn't seem to be affect anything. I also exported $LD_LIBRARY_PATH="/usr/local/lib" (/usr/local/bin is where libcsfml-audio.dylib is.), and tried compilation through

nim c alarm.nim --clib:/usr/local/lib/libcsfml-audio.dylib

Thanks for the help!


回答1:


This program would just exit immediately; you need to keep it alive while the sound plays. Append this to the program:

import csfml_system

while alarmsong.status == SoundStatus.Playing:
  sleep 100.milliseconds



回答2:


For nim-csfml to work you'll need SFML 2.1 and CSFML 2.1. Also, it seems that nim-csfml is actually broken for Mac OS X, so I've made a pull request with a fix: https://github.com/BlaXpirit/nim-csfml/pull/4

Other modules that could play sound are sdl_mixer, sdl2/audio and allegro5.

As an OSX-only alternative without using any libraries, by calling the afplay binary:

import osproc
discard execProcess("afplay", ["file.wav"])

Edit1:

When Nim reports "could not load: libcsfml-audio.dynlib" that could also mean that one of the dependencies of that library are missing or in a wrong version. Especially SFML 2.2 doesn't work with CSFML 2.1. Make sure libsfml-audio.dynlib is in your LD_LIBRARY_PATH as well. If that doesn't work either, you could try to compile and run a regular C CSFML example like this one: https://gist.github.com/def-/fee8bb041719337c8812

Compile it with clang -o mainpage -lcsfml-graphics -lcsfml-audio -lGL -lGLEW mainpage.c to see the errors/warnings about missing libraries.



来源:https://stackoverflow.com/questions/28596986/what-is-a-simple-way-to-play-a-wav-file-in-nim-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!