BASS & BASSMIDI not loadin correctly

Started by Giraud,

Giraud

Hello from da country side,

Exception has occurred: RuntimeError
Cannot load SoundFont. BASS error=2
  File "C:\Users\Asus\Desktop\Sarabande\sarabande.py", line 64, in <module>
    raise RuntimeError(f"Cannot load SoundFont. BASS error={bass.BASS_ErrorGetCode()}")
RuntimeError: Cannot load SoundFont. BASS error=2

Why zis not work please:

from ctypes import *
import os
import time
import platform

BASS_UNICODE = 0x80000000

HERE = os.path.dirname(os.path.abspath(__file__))
SOUNDFONT = os.path.join(HERE, "GeneralUser GS v1.471.sf2")
MIDI_FILE = os.path.join(HERE, "bach_sarabande.mid")

bass = WinDLL(os.path.join(HERE, "bass.dll"))
bassmidi = WinDLL(os.path.join(HERE, "bassmidi.dll"))

bass.BASS_Init.argtypes = [c_int, c_uint, c_uint, c_void_p, c_void_p]
bass.BASS_Init.restype = c_bool

bass.BASS_Free.restype = c_bool

bass.BASS_ChannelPlay.argtypes = [c_uint, c_bool]
bass.BASS_ChannelPlay.restype = c_bool

bass.BASS_ChannelIsActive.argtypes = [c_uint]
bass.BASS_ChannelIsActive.restype = c_uint

bass.BASS_ErrorGetCode.argtypes = []
bass.BASS_ErrorGetCode.restype = c_int

bass.BASS_GetVersion.restype = c_uint
bassmidi.BASS_MIDI_GetVersion.restype = c_uint

bassmidi.BASS_MIDI_FontInit.argtypes = [c_wchar_p, c_uint]
bassmidi.BASS_MIDI_FontInit.restype = c_uint

bassmidi.BASS_MIDI_StreamCreateFile.argtypes = [
    c_bool, c_wchar_p, c_uint, c_uint, c_uint, c_uint
]
bassmidi.BASS_MIDI_StreamCreateFile.restype = c_uint

class BASS_MIDI_FONTEX(Structure):
    _fields_ = [("font", c_uint), ("preset", c_int), ("bank", c_int)]

bassmidi.BASS_MIDI_StreamSetFonts.argtypes = [
    c_uint, POINTER(BASS_MIDI_FONTEX), c_uint
]
bassmidi.BASS_MIDI_StreamSetFonts.restype = c_bool

print("Python architecture:", platform.architecture())
print("HERE              :", HERE)
print("SoundFont exists  :", os.path.exists(SOUNDFONT))
print("MIDI exists       :", os.path.exists(MIDI_FILE))
print("SoundFont path    :", SOUNDFONT)
print("MIDI path         :", MIDI_FILE)
print("bass.dll          :", bass._name)
print("bassmidi.dll      :", bassmidi._name)
print("BASS version      :", hex(bass.BASS_GetVersion()))
print("BASSMIDI version  :", hex(bassmidi.BASS_MIDI_GetVersion()))

if not bass.BASS_Init(-1, 44100, 0, None, None):
    raise RuntimeError(f"Cannot initialize BASS. Error={bass.BASS_ErrorGetCode()}")

font = bassmidi.BASS_MIDI_FontInit(SOUNDFONT, 0)
if font == 0:
    raise RuntimeError(f"Cannot load SoundFont. BASS error={bass.BASS_ErrorGetCode()}")

stream = bassmidi.BASS_MIDI_StreamCreateFile(
    False, MIDI_FILE, 0, 0, 0, 44100
)
if stream == 0:
    raise RuntimeError(f"Cannot load MIDI file. BASS error={bass.BASS_ErrorGetCode()}")

fonts = BASS_MIDI_FONTEX(font, -1, 0)

if not bassmidi.BASS_MIDI_StreamSetFonts(stream, byref(fonts), 1):
    raise RuntimeError(f"Cannot assign SoundFont. BASS error={bass.BASS_ErrorGetCode()}")

if not bass.BASS_ChannelPlay(stream, False):
    raise RuntimeError(f"Cannot play stream. BASS error={bass.BASS_ErrorGetCode()}")

print("Playing...")

while bass.BASS_ChannelIsActive(stream):
    time.sleep(0.1)

bass.BASS_Free()
print("Done.")



Python architecture: ('64bit', 'WindowsPE')
HERE              : C:\Users\Asus\Desktop\Sarabande
SoundFont exists  : True
MIDI exists       : True
SoundFont path    : C:\Users\Asus\Desktop\Sarabande\GeneralUser GS v1.471.sf2
MIDI path         : C:\Users\Asus\Desktop\Sarabande\bach_sarabande.mid
bass.dll          : C:\Users\Asus\Desktop\Sarabande\bass.dll
bassmidi.dll      : C:\Users\Asus\Desktop\Sarabande\bassmidi.dll
BASS version      : 0x2041203
BASSMIDI version  : 0x2041000

Ian @ un4seen

That looks like BASS and BASSMIDI are loaded, but BASS_MIDI_FontInit is failing with error code 2? Error code 2 = BASS_ERROR_FILEOPEN, which means the file couldn't be opened (usually because it doesn't exist). I notice you have "c_wchar_p" in your BASS_MIDI_FontInit declaration. If the filename is Unicode, then you should include the BASS_UNICODE flag in the call:

font = bassmidi.BASS_MIDI_FontInit(SOUNDFONT, BASS_UNICODE)

The same thing applies to the BASS_MIDI_StreamCreateFile call.