Cross-platform sound playing
Posted: Sun Nov 24, 2024 1:58 pm
Because the play sound command doesn't work in Linux, here's an example of how a cross-platform version can work.
A forum for xTalk discussion
https://openxtalk.org/forum/
Yes, ignore the difference in UI. That would be the case in Windows too, where it looks different again. Not to worry about that though (within this topic ) - it's just to give you a method to vary the sound volume.richmond62 wrote: ↑Sun Nov 24, 2024 2:08 pm As you can see from my screenshot, it looks a bit different to yours (presumably on some type of Linux), so NOT cross-platform in terms of appearance.
A bit of both. The xPlaySound function is in the stack script.richmond62 wrote: ↑Sun Nov 24, 2024 2:12 pm I assume xPlaySound(1,2) is your invention.
Is that a custom something in your stack, or will that be integrated into 1.10?
Okay Not that these are 'Hacks' though.richmond62 wrote: ↑Sun Nov 24, 2024 2:23 pm ...I decompressed your thing inside a folder on my desktop called 'Hax 23 November'.
The Play [soundFile] command built-into the engine only 'sort-of' works on macOS, it will play basic .wav, .aif, and probably .au (common on old Unix) sound files, but once it starts playing there's no way to stop it, 'play Stop' doesn't work, and if you pass it an mp3 it will try to play it but you get horrible noises instead. I've used command line 'afplay' that comes with macOS to play sounds in the background before, but with Extension Builder I've tapped into NSSound & AVAudio frameworks directly, that includes sound recording, which is a feature I'd personally like to have available on all platforms.
Rigfht, soLinux
The minimal requirements for LiveCode to run on Linux are:
32-bit installation, or a 64-bit linux distribution that has a 32-bit compatibility layer
2.4.x or later kernel
glibc 2.3.2 or later X11R5 capable Xserver running locally on a 24-bit display
compositing window manager (optional – required for alpha-blended window shapes)
gtk/gdk/glib (optional – required for native theme support)
pango/xft (optional – required for pdf printing, anti-aliased text and unicode font support)
lcms (optional – required for color profile support in JPEGs and PNGs)
gksu (optional – required for elevate process support)
mplayer (optional – required for video playback)
esd (optional – required for audio playback)
Although impossible to test every existing Linux distribution, we are aiming to ensure that LiveCode runs on as wide a variety of systems as possible. To achieve this, the engine has been implemented to have minimal direct dependencies on system software, and will gracefully degrade in feature set if it cannot find the libraries it needs. Generally any recent linux distribution including Gnome/GTK support will have the required libraries for full feature support ñ for example, Ubuntu 7 supports all these features (although alpha blended window shape support requires you to be running with ‘Advance Desktop Effects’ turned on).
Code: Select all
$ xanim '/home/Paul/Desktop/Ocean.mp3'
XAnim Rev 2.80.0 by Mark Podlipec Copyright (C) 1991-1999. All Rights Reserved
Unknown or unsupported animation type: /home/Paul/Desktop/Ocean.mp3
Usage:
XAnim [options] anim [ [options] anim ... ]
-h lists some common options, but may be out of date.
See xanim.readme or the man page for detailed help.
Paul@6430sMX:~
$ xanim '/home/Paul/Desktop/Stream.wav'
XAnim Rev 2.80.0 by Mark Podlipec Copyright (C) 1991-1999. All Rights Reserved
Can't Open /dev/dsp device
Code: Select all
function xplaySound tSound, tLoud
-- calculate path to sound file
set the itemdelimiter to slash
put the long name of this stack into tPath
if word 1 of tPath is "stack" then delete word 1 of tPath
replace QUOTE with "" in tPath
delete last item of tPath
put QUOTE & tPath & slash & "audio" & slash & tSound & QUOTE into tAudioFile
-- conditional play command
if the platform contains "lin" then
put "mplayer -really-quiet -volume " & tLoud & " " & tAudioFile into tCmd
put tCmd & " > /dev/null 2>/dev/null &" into tNoWaitShellCommand
get shell(tNoWaitShellCommand)
else
set the playLoudness to tLoud -- does not work for linux, but "should" work for windows and macOs
do "play " & tAudioFile
end if
end xplaySound
Code: Select all
if shell("which mplayer") is not empty
Code: Select all
if shell("command -v mplayer") is not empty
Code: Select all
dpkg -s "[thing]"
This method assumes user is running a Debian based Linux Disto or otherwise has .deb package manager, some distros like Fedora or Arch Linux use different package managers.tperry2x wrote: ↑Fri Nov 29, 2024 10:43 am Yes, that's a good point - although, that's especially true for any of the linux-type libraries we are adding.
You could use the same method I use to check dependencies in this stack if you like (which is essentially the method I use for Linux in the 'compatibility' card of the Preferences)
Short version: I'm using:So, dpkg -s "flex" where "flex" is the name of what I'm checking for of course.Code: Select all
dpkg -s "[thing]"
Code: Select all
aplay mysound.wav
Code: Select all
aplay -D hw:0,0 -c 2 mysound.wav
Code: Select all
aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: 92HD93BXX Analog [92HD93BXX Analog]
Subdevices: 0/1
Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
Subdevices: 0/1
Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1]
Subdevices: 0/1
Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 8: HDMI 2 [HDMI 2]
Subdevices: 0/1
Subdevice #0: subdevice #0
Code: Select all
aplay -L
null
Discard all samples (playback) or generate zero samples (capture)
lavrate
Rate Converter Plugin Using Libav/FFmpeg Library
samplerate
Rate Converter Plugin Using Samplerate Library
speexrate
Rate Converter Plugin Using Speex Resampler
jack
JACK Audio Connection Kit
oss
Open Sound System
pipewire
PipeWire Sound Server
pulse
PulseAudio Sound Server
speex
Plugin using Speex DSP (resample, agc, denoise, echo, dereverb)
upmix
Plugin for channel upmix (4,6,8)
vdownmix
Plugin for channel downmix (stereo) with a simple spacialization
default
Default ALSA Output (currently PipeWire Media Server)
hw:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
Direct hardware device without any conversions
hw:CARD=PCH,DEV=3
HDA Intel PCH, HDMI 0
Direct hardware device without any conversions
hw:CARD=PCH,DEV=7
HDA Intel PCH, HDMI 1
Direct hardware device without any conversions
hw:CARD=PCH,DEV=8
HDA Intel PCH, HDMI 2
Direct hardware device without any conversions
plughw:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
Hardware device with all software conversions
plughw:CARD=PCH,DEV=3
HDA Intel PCH, HDMI 0
Hardware device with all software conversions
plughw:CARD=PCH,DEV=7
HDA Intel PCH, HDMI 1
Hardware device with all software conversions
plughw:CARD=PCH,DEV=8
HDA Intel PCH, HDMI 2
Hardware device with all software conversions
sysdefault:CARD=PCH
HDA Intel PCH, 92HD93BXX Analog
Default Audio Device
front:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
Front output / input
surround21:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
4.0 Surround output to Front and Rear speakers
surround41:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
hdmi:CARD=PCH,DEV=0
HDA Intel PCH, HDMI 0
HDMI Audio Output
hdmi:CARD=PCH,DEV=1
HDA Intel PCH, HDMI 1
HDMI Audio Output
hdmi:CARD=PCH,DEV=2
HDA Intel PCH, HDMI 2
HDMI Audio Output
dmix:CARD=PCH,DEV=0
HDA Intel PCH, 92HD93BXX Analog
Direct sample mixing device
dmix:CARD=PCH,DEV=3
HDA Intel PCH, HDMI 0
Direct sample mixing device
dmix:CARD=PCH,DEV=7
HDA Intel PCH, HDMI 1
Direct sample mixing device
dmix:CARD=PCH,DEV=8
HDA Intel PCH, HDMI 2
Direct sample mixing device
usbstream:CARD=PCH
HDA Intel PCH
USB Stream Output