FIXED: Call Library from Python - no sound (but also no errors)

Multi-platform modular music creation studio
Post Reply
nardj
Posts: 4
Joined: Sun Nov 29, 2020 9:29 pm

FIXED: Call Library from Python - no sound (but also no errors)

Post by nardj »

Hi all,

I am trying to play a sunvox file using python 3.8.5 and the SunVox Library 1.9.6b and linking them using the standard ctypes library from python. The python script produces no errors, the library functions return no errors. However there is no sound :( (The loaded file plays fine with the full SunVox GUI)
What am I doing wrong?

Code: Select all

import ctypes
import time

# get script directory
import os
scriptpath= os.path.realpath(__file__) 
scriptdir = os.path.dirname(scriptpath)
# construct full path to sunvox lib
libname=os.path.join(scriptdir,"sunvox.so")

if __name__ == "__main__":
    # Load the shared library into ctypes
    svlib = ctypes.CDLL(libname)

    # CONNECT TO SOUND SYSTEM
    svlib.sv_init.restype=ctypes.c_int32
    ver = svlib.sv_init(None, 44100, 2, 0 )
    print (f"Init Sound succeeded!") if ver>=0 else print (f"Link Sound failed, error:{ver}")

    if( ver >= 0 ):
        # REQUEST SLOT
        slotnr=0
        success=svlib.sv_open_slot(slotnr)
        print (f"Open slot succeeded!") if success==0 else print (f"Open slot failed, error:{success}")

        # LOAD FILE
        svfile=os.path.join(scriptdir,"test.sunvox")
        success = svlib.sv_load(slotnr, ctypes.c_wchar_p(svfile))
        print (f"Open file succeeded!") if success==0 else print (f"Open file failed, error:{success}")
        
        # SET VOLUME
        svlib.sv_volume(slotnr,256)

        # START PLAY
        success = svlib.sv_play_from_beginning(slotnr)
        print (f"Play file succeeded!") if success==0 else print (f"Play file failed, error:{success}")

        # LET PLAY FOR 5 SECONDS
        time.sleep(5)

        # STOP PLAY
        svlib.sv_stop(slotnr)

        # CLOSE SLOT
        svlib.sv_close_slot(slotnr)

        # RELEASE SOUND SYSTEM
        svlib.sv_deinit()
Cheers, Nard
Last edited by nardj on Sun Nov 29, 2020 11:56 pm, edited 1 time in total.
User avatar
NightRadio
Site Admin
Posts: 3944
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Call Library from Python - no sound (but also no errors)

Post by NightRadio »

Are there any messages from sunvox library in stdout (console)?
nardj
Posts: 4
Joined: Sun Nov 29, 2020 9:29 pm

Re: Call Library from Python - no sound (but also no errors)

Post by nardj »

Yes there are:

Code: Select all

ALSA: pulse
ALSA HW Default rate: 44100
ALSA HW Rate: 44100 frames
ALSA HW Buffer size: 4096 frames
ALSA HW Period size: 227
ALSA HW Periods: 0
ALSA SW Avail min: 227
ALSA SW Start threshold: 1
ALSA SW Stop threshold: 4096
Init Sound succeeded!
Open slot succeeded!
Open file succeeded!
Play file succeeded!
SOUND: sundog_sound_deinit() begin
SOUND: sundog_sound_deinit() end
Max memory used: 1826543
nardj
Posts: 4
Joined: Sun Nov 29, 2020 9:29 pm

Re: Call Library from Python - no sound (but also no errors)

Post by nardj »

Fixed it!

Error was in passing filename to sv_load. Filename should not be passed as ctypes.c_wchar_p but as ctypes.c_char_p
Strange no error was given by library.

Code: Select all

        svfile=os.path.join(scriptdir,"test.sunvox")
        bsvfile = svfile.encode('utf-8')
        success = svlib.sv_load(slotnr, ctypes.c_char_p(bsvfile))
 
and full working code below

Code: Select all

import ctypes
import time

# get script directory
import os
scriptpath= os.path.realpath(__file__) 
scriptdir = os.path.dirname(scriptpath)
# construct full path to sunvox lib
libname=os.path.join(scriptdir,"sunvox.so")

if __name__ == "__main__":
    # Load the shared library into ctypes
    svlib = ctypes.CDLL(libname)

    # CONNECT TO SOUND SYSTEM
    svlib.sv_init.restype=ctypes.c_int32
    ver = svlib.sv_init(None, 44100, 2, 0 )
    print (f"Init Sound succeeded!") if ver>=0 else print (f"Link Sound failed, error:{ver}")

    if( ver >= 0 ):
        # REQUEST SLOT
        slotnr=0
        success=svlib.sv_open_slot(slotnr)
        print (f"Open slot succeeded!") if success==0 else print (f"Open slot failed, error:{success}")

        # LOAD FILE
        svfile=os.path.join(scriptdir,"test.sunvox")
        bsvfile = svfile.encode('utf-8')
        success = svlib.sv_load(slotnr, ctypes.c_char_p(bsvfile))
        print (f"Open file succeeded!") if success==0 else print (f"Open file failed, error:{success}")
        
        # SET VOLUME
        svlib.sv_volume(slotnr,256)

        # START PLAY
        success = svlib.sv_play_from_beginning(slotnr)
        print (f"Play file succeeded!") if success==0 else print (f"Play file failed, error:{success}")

        # LET PLAY FOR 5 SECONDS
        time.sleep(5)

        # STOP PLAY
        svlib.sv_stop(slotnr)

        # CLOSE SLOT
        svlib.sv_close_slot(slotnr)

        # RELEASE SOUND SYSTEM
        svlib.sv_deinit()
nardj
Posts: 4
Joined: Sun Nov 29, 2020 9:29 pm

Re: Call Library from Python - no sound (but also no errors)

Post by nardj »

Maybe this working code could be placed on https://warmplace.ru/soft/sunvox/sunvox_lib.php web page in section Hello SunVox Lib?
User avatar
NightRadio
Site Admin
Posts: 3944
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: FIXED: Call Library from Python - no sound (but also no errors)

Post by NightRadio »

Error was in passing filename to sv_load. Filename should not be passed as ctypes.c_wchar_p but as ctypes.c_char_p
Strange no error was given by library.
Yes, it's strange...

Maybe this working code could be placed on https://warmplace.ru/soft/sunvox/sunvox_lib.php web page in section Hello SunVox Lib?
Definitely! Thank you!
Post Reply