20 May '13 - 07:09 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: BASS Midi and Autohotkey  (Read 2273 times)
Lemo
Posts: 11


« on: 12 Jun '11 - 02:18 »
Reply with quoteQuote

Hi all


I'm doing a "random cloud Midi player" with Autohotkey (http://www.autohotkey.com) and I recently heard about BASS and its Midi plugin.
For the moment, I'm using the basic autohotkey command "Soundplay". It plays fine with midi, but you don't have any control over the playback,
or any access to midi stats.

I would like to use BASS and the script posted here:
http://www.autohotkey.com/forum/topic37092.html
Unfortunately the author has left the forum, and doesn't answer to private messages either...
I'm not getting any support on that thread, so I thought I'd post around here

His script seems to work with a simple wav or mp3, but the midi plugin just doesn't seem to produce any sound, even with default soundfont.
(You may see my attemps at the end of the topic above)
I checked the C++ examples and help files included with BASS, but I'd say the examples are too complex and the doc isn't enough.
Also I'm not a dev and all of this is way out of my scope^^'.
All I would need I guess is a sample code (~20lines) of a very basic midi loader, that I may be able to translate to autohotkey.
Or if anyone know about autohotkey, of course it's even better if the example is in that language directly.

The link on the forum is dead, so here is where I found the scripts for autohotkey
http://www.autohotkey.net/file/users/Members/k3ph/bass/trunk/

Thanks for your help!
Lemo
Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #1 on: 13 Jun '11 - 15:23 »
Reply with quoteQuote

I'm afraid I can't help with Autohotkey scripting, but the basic C/C++ code to set a soundfont and play a MIDI file would look something like this...

BASS_SetConfigPtr(BASS_CONFIG_MIDI_DEFFONT, sf2_filename); // set default soundfont
midi=BASS_MIDI_StreamCreateFile(FALSE, midi_filename, 0, 0, 0, 0); // load a MIDI file
BASS_ChannelPlay(midi, FALSE); // start playing it

To check whether there is a default soundfont set, you can use BASS_GetConfigPtr to check the BASS_CONFIG_MIDI_DEFFONT value (NULL = no soundfont). It will default to a Creative soundfont, if available.
Logged
Lemo
Posts: 11


« Reply #2 on: 15 Jun '11 - 04:07 »
Reply with quoteQuote

Thanks for the help!

I've tried this and apparently it doesn't work for autohotkey from the first line
What's the difference between BASS_SetConfigPtr and BASS_MIDI_StreamSetFonts?
Here is how the test looks like
#include bass.ahk

BASS_Load()
BASS_Init()
BASS_MIDI_Init()

font1:=BASS_GetConfigPtr(BASS_CONFIG_MIDI_DEFFONT)
BASS_SetConfigPtr(BASS_CONFIG_MIDI_DEFFONT, CT4MGM.SF2) ; // set default soundfont
font2:=BASS_GetConfigPtr(BASS_CONFIG_MIDI_DEFFONT)
msgbox, %font1% %font2%
return

exit:
  BASS_Free()
  BASS_MIDI_Free()
ExitApp
font1 returns 0
font2 returns 0
:\

Bass.ahk is actually just a library for autohotkey that sends calls to bass.dll
For exemple this part of Bass.ahk
BASS_SetConfig(option,value){
global
Return DllCall(BASS_DLLPATH . BASS_DLL . "\BASS_SetConfig", UInt, option, UInt, value)
}

If it can help, I've posted a version of a autohotkey script that works for files like mp3 here:
http://www.autohotkey.com/forum/viewtopic.php?p=451956#451956

I also have a copy of the default? CT4MGM.SF2 in C:\WINDOWS\system32 from the beginning btw
« Last Edit: 15 Jun '11 - 04:16 by Lemo » Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #3 on: 15 Jun '11 - 16:11 »
Reply with quoteQuote

What's the difference between BASS_SetConfigPtr and BASS_MIDI_StreamSetFonts?

Both can be used to set the default soundfont(s). BASS_SetConfigPtr (BASS_CONFIG_MIDI_DEFFONT) is the simpler way to do it if a single soundfont is being used to provide all presets. BASS_MIDI_StreamSetFonts can be used for more complex (as well as file-specific) soundfont configurations.

I also have a copy of the default? CT4MGM.SF2 in C:\WINDOWS\system32 from the beginning btw

In that case, BASSMIDI should use it automatically without you having to tell it to (eg. via the BASS_CONFIG_MIDI_DEFFONT option). To confirm whether BASSMIDI is finding the soundfont, is the precompiled MIDITEST.EXE example (in the C\BIN directory) using it by default?
Logged
Lemo
Posts: 11


« Reply #4 on: 16 Jun '11 - 03:14 »
Reply with quoteQuote

Yes Miditest.exe works with that soundfont in system32, and stops loading it as default when I remove it.

I dunno if it can find it through autohotkey though...

Maybe there is something wrong (in ahk) with the init then?

On autohotkey, apparently it's supposed to look like that:
BASS_Load()
BASS_Init()
BASS_MIDI_Init()

Which refers in the provided bass.ahk and bassmidi.ahk to
BASS_Load(){
global
BASS_DLLCALL := DllCall("LoadLibrary", Str, BASS_DLLPATH . BASS_DLL)
}
BASS_Init(device=-1,freq=44100,flags=0,win=0,clsid=0){
global
Return DllCall(BASS_DLLPATH . BASS_DLL . "\BASS_Init", Int, device, Int, freq, Int, flags, UInt, win, UInt, clsid)
}
BASS_MIDI_Init(){
global
BASS_MIDI_LOADED := 1
BASS_MIDI_HANDLE := BASS_PluginLoad(BASS_DLL_PLUGINPATH . BASS_DLL_MIDI, 0)
BASS_MIDI_DLLCALL := DllCall("LoadLibrary", Str, BASS_DLL_PLUGINPATH . BASS_DLL_MIDI)
}

Also I don't see any BASS_Init() in your sample C++ code, is it optional?

Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #5 on: 16 Jun '11 - 14:46 »
Reply with quoteQuote

Yes Miditest.exe works with that soundfont in system32, and stops loading it as default when I remove it.

I dunno if it can find it through autohotkey though...

That shouldn't be a problem, ie. Autohotkey won't affect BASSMIDI's ability to find the soundfont.

Maybe there is something wrong (in ahk) with the init then?

On autohotkey, apparently it's supposed to look like that:
BASS_Load()
BASS_Init()
BASS_MIDI_Init()

Which refers in the provided bass.ahk and bassmidi.ahk to
BASS_Load(){
global
BASS_DLLCALL := DllCall("LoadLibrary", Str, BASS_DLLPATH . BASS_DLL)
}
BASS_Init(device=-1,freq=44100,flags=0,win=0,clsid=0){
global
Return DllCall(BASS_DLLPATH . BASS_DLL . "\BASS_Init", Int, device, Int, freq, Int, flags, UInt, win, UInt, clsid)
}
BASS_MIDI_Init(){
global
BASS_MIDI_LOADED := 1
BASS_MIDI_HANDLE := BASS_PluginLoad(BASS_DLL_PLUGINPATH . BASS_DLL_MIDI, 0)
BASS_MIDI_DLLCALL := DllCall("LoadLibrary", Str, BASS_DLL_PLUGINPATH . BASS_DLL_MIDI)
}

Also I don't see any BASS_Init() in your sample C++ code, is it optional?

No, the BASS_Init call is required. I notice you're using win=0 in the call. That could be a problem. Do you have a window handle that you could use there?

Is it only MIDI that you're having trouble with, ie. are you able to successfully play MP3/OGG/WAV files? Also be sure to check the return value of each of your BASS function calls to check whether they are succeeding, and if any isn't, use BASS_ErrorGetCode to find out why.
Logged
Lemo
Posts: 11


« Reply #6 on: 17 Jun '11 - 03:41 »
Reply with quoteQuote

I'm able to play mp3/waw/ogg indeed

Quote from: Lemo
If it can help, I've posted a version of a autohotkey script that works for files like mp3 here:
http://www.autohotkey.com/forum/viewtopic.php?p=451956#451956

When you don't create a GUI, usually autohotkey runs by default as a small tray icon,
then I suppose it's like a "console application" as mentioned in BASS help file (win=0)
(again, bass.ahk and bassmidi.ahk were created by k3ph and I don't understand everything happening in those files)

Thanks for the BASS_ErrorGetCode idea, I tried that before, but apparently I forgot to test the init, here are the results
BASS_Load() -> 0
BASS_Init() -> 0
BASS_MIDI_Init() -> 2

If that's a BASS_ERROR_FILEOPEN with the bassmidi plugin, any idea what may go wrong in there?
Quote from: k3ph for bassmidi.ahk
BASS_MIDI_Init(){
   global
   BASS_MIDI_LOADED := 1
   BASS_MIDI_HANDLE := BASS_PluginLoad(BASS_DLL_PLUGINPATH . BASS_DLL_MIDI, 0)
   BASS_MIDI_DLLCALL := DllCall("LoadLibrary", Str, BASS_DLL_PLUGINPATH . BASS_DLL_MIDI)
}
I have everything in the same folder: autohotkey script, bass.ahk, bassmidi.ahk, midi file, bass.dll, bassmidi.dll
Maybe I have to set that "PLUGINPATH" ?

« Last Edit: 17 Jun '11 - 03:52 by Lemo » Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #7 on: 17 Jun '11 - 15:00 »
Reply with quoteQuote

BASS_ERROR_FILEOPEN indicates that the requested file/plugin could not be loaded. What are "BASS_DLL_PLUGINPATH" and "BASS_DLL_MIDI" set to, and does that look correct?

Btw, the LoadLibrary call is unnecessary as BASSMIDI.DLL will already be loaded by the preceding BASS_PluginLoad call (if successful).
Logged
Lemo
Posts: 11


« Reply #8 on: 17 Jun '11 - 22:42 »
Reply with quoteQuote

Okay I found those "BASS_DLL_PLUGINPATH" and "BASS_DLL_MIDI" in the bass.ahk script
I think it looks correct

BASS_DLL_PLUGINPATH := A_ScriptDir . "\"
BASS_DLL_MIDI  := "bassmidi.dll"

Also I just tried to replace
   BASS_MIDI_HANDLE := BASS_PluginLoad(BASS_DLL_PLUGINPATH . BASS_DLL_MIDI, 0)
with
   BASS_MIDI_HANDLE := BASS_PluginLoad(BASS_DLL_PLUGINPATH . BASS_DLL_MIDI, BASS_UNICODE)

...and BASS_MIDI_Init() now returns 0 ! Smiley
I'm rather new with those unicode problems, does it mean bassmidi.dll was in unicode, or that there were something weird with the characters in its path?
When I open it with Notepad++, apparently it says it's encoded in ANSI?

Anyway I still have no sound ^^'
I'll investigate more later
Logged
Lemo
Posts: 11


« Reply #9 on: 18 Jun '11 - 02:27 »
Reply with quoteQuote

As the init is "probably" fine now, I came back to the other lines:

midi:=BASS_MIDI_StreamCreateFile(FALSE, test.mid, 0, 0, 0, 0) ; // load a MIDI file
BASS_ChannelPlay(midi, FALSE) ; // start playing it
(Which refers to:)
BASS_MIDI_StreamCreateFile(mem,file,offset,length,flags,freq){
global
Return DllCall(BASS_DLL_PLUGINPATH . BASS_DLL_MIDI . "\BASS_MIDI_StreamCreateFile", UInt, mem, UInt, file,

UInt64, offset, UInt64, length, UInt, flags, UInt, freq)
}
BASS_ChannelPlay(handle,restart=0){
global
Return DllCall(BASS_DLLPATH . BASS_DLL . "\BASS_ChannelPlay", UInt, handle, Int, restart)
}

I get "0" as error code for StreamCreateFile, but it's weird because if I change to a file that doesn't exist in the folder,
like test000.mid, I'm not getting any error either...

ChannelPlay gives an error "5" (BASS_ERROR_HANDLE)
Quote from: Doc
handle is not a valid channel
That means the midi file wasn't loaded properly?


« Last Edit: 18 Jun '11 - 02:35 by Lemo » Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #10 on: 20 Jun '11 - 15:33 »
Reply with quoteQuote

As you're loading the BASSMIDI.DLL as a plugin (via BASS_PluginLoad), you could try using BASS_StreamCreateFile (rather than BASS_MIDI_StreamCreateFile) to load the MIDI file, as you have successfully done with MP3/OGG/etc.
Logged
Lemo
Posts: 11


« Reply #11 on: 22 Jun '11 - 05:36 »
Reply with quoteQuote

Still no luck :\
I'm getting error 2 with BASS_StreamCreateFile
Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #12 on: 22 Jun '11 - 16:21 »
Reply with quoteQuote

Is the same BASS_StreamCreateFile call still working for non-MIDI files, ie. if you just replace the filename?

Error code 2 is BASS_ERROR_FILEOPEN, which indicates that the file couldn't be opened, usually because it doesn't exist (could also be due to lack of permission). Are you using the BASS_UNICODE flag? You mentioned earlier that it fixed the BASS_PluginLoad call, in which case, it's probably needed here too.
Logged
Lemo
Posts: 11


« Reply #13 on: 23 Jun '11 - 03:12 »
Reply with quoteQuote

Yea I tried that flag at a few spots before

With BASS_MIDI_StreamCreateFile and BASS_UNICODE, I still have -> error 2

With BASS_StreamCreateFile and BASS_UNICODE, I have an error 41 BASS_ERROR_FILEFORM


That means the MIDI format hasn't been setup successfully with BASS_PluginLoad?
Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #14 on: 23 Jun '11 - 13:57 »
Reply with quoteQuote

It could do. Does the same MIDI file play OK with the pre-compiled MIDITEST.EXE example? If so, are you sure that the BASS_PluginLoad call was successful? It should return a non-0 value.
Logged
Lemo
Posts: 11


« Reply #15 on: 23 Jun '11 - 17:15 »
Reply with quoteQuote

Okay my bad I forgot to mention I tried an install of the previous "basic" version of Autohotkey instead of Autohotkey_L
The basic version doesn't have support for Unicode while the _L has
(http://www.autohotkey.com/download/)

It doesn't make any change to my tests, except I have to remove BASS_UNICODE from BASS_PluginLoad (as it was originally)
I had indeed an error 2 with BASS_UNICODE still there

Now:
BASS_PluginLoad -> 0
BASS_StreamCreateFile (no flags) -> 2

Weird thing is that if I try to add again BASS_UNICODE in BASS_StreamCreateFile, I'm getting error 0
But after that I still have that error 5 in BASS_ChannelPlay

MIDI file plays OK with the pre-compiled MIDITEST.EXE
« Last Edit: 23 Jun '11 - 17:19 by Lemo » Logged
Ian @ un4seen
Administrator
Posts: 15244


« Reply #16 on: 24 Jun '11 - 12:20 »
Reply with quoteQuote

BASS_PluginLoad -> 0

Just to be sure, is that the error code or the return value? In either case, please also check the other.

Are you able to play MP3/OGG/etc files with your current code, by changing the filename that you pass to BASS_StreamCreateFile and nothing else?
Logged
Lemo
Posts: 11


« Reply #17 on: 27 Jun '11 - 22:17 »
Reply with quoteQuote

Yes that's the error code
BASS_PluginLoad returns 43122688

Actually emmanuel from autohotkey forums finally managed to get BASSmidi working in the meantime
In his case it was just because of the lack of C:\WINDOWS\system32\CT4MGM.SF2
I have the soundfont there since the begining, so he gave me his script, with the dlls, soundfont, midi file and autohotkey version he uses,
but it just still doesn't work for me
http://www.autohotkey.com/forum/viewtopic.php?p=454689#454689

#SingleInstance force

BASS_DLLCALL := DllCall("LoadLibrary","str",A_ScriptDir "\bass.dll")
BASS_ErrorGetCode1 := DllCall(A_ScriptDir "\bass.dll\BASS_ErrorGetCode", Int)
BASS_MIDI_DLLCALL := DllCall("LoadLibrary", "str",A_ScriptDir "\bassmidi.dll")
BASS_ErrorGetCode2 := DllCall(A_ScriptDir "\bass.dll\BASS_ErrorGetCode", Int)
BASS_Init := DllCall(A_ScriptDir . "\bass.dll\BASS_Init",Int,-1,Int,44100,Int,0,UInt,0,UInt,0)
BASS_ErrorGetCode3 := DllCall(A_ScriptDir "\bass.dll\BASS_ErrorGetCode", Int)
BASS_MIDI_HANDLE := DllCall(A_ScriptDir "\bass.dll\BASS_PluginLoad", "str", A_ScriptDir "\bassmidi.dll", UInt, 0)
BASS_ErrorGetCode4 := DllCall(A_ScriptDir "\bass.dll\BASS_ErrorGetCode", Int)
File :=A_ScriptDir "\test Beethoven's Fur Elise.mid"
BASS_MIDI_StreamCreateFile:=hMedia:= DllCall(A_ScriptDir "\bassmidi.dll\BASS_MIDI_StreamCreateFile", UInt, False, UInt, &file, UInt64, 0, UInt64, 0, UInt, 0, UInt, 44100)
BASS_ErrorGetCode5 := DllCall(A_ScriptDir "\bass.dll\BASS_ErrorGetCode", Int)
BASS_ChannelPlay :=DllCall(A_ScriptDir . "\bass.dll\BASS_ChannelPlay", UInt,hMedia, Int,1, Cdecl Int)
BASS_ErrorGetCode6 := DllCall(A_ScriptDir "\bass.dll\BASS_ErrorGetCode", Int)
clipboard=
(
BASS_DLLCALL: %BASS_DLLCALL%
BASS_ErrorGetCode1: %BASS_ErrorGetCode1%
BASS_MIDI_DLLCALL: %BASS_MIDI_DLLCALL%
BASS_ErrorGetCode2: %BASS_ErrorGetCode2%
BASS_Init: %BASS_Init%
BASS_ErrorGetCode3: %BASS_ErrorGetCode3%
BASS_MIDI_HANDLE: %BASS_MIDI_HANDLE%
BASS_ErrorGetCode4: %BASS_ErrorGetCode4%
File: %File%
BASS_MIDI_StreamCreateFile: %BASS_MIDI_StreamCreateFile%
BASS_ErrorGetCode5: %BASS_ErrorGetCode5%
BASS_ChannelPlay: %BASS_ChannelPlay%
BASS_ErrorGetCode6: %BASS_ErrorGetCode6%
)

return
esc::
DllCall(A_ScriptDir "\bass.dll\BASS_PluginFree", UInt, BASS_WMA_HANDLE)
DllCall(A_ScriptDir "\bass.dll\BASS_Free")
DllCall("FreeLibrary", UInt, BASS_MIDI_DLLCALL)
DllCall("FreeLibrary", UInt, BASS_DLLCALL)
reload
return

Quote
BASS_DLLCALL: 19070976
BASS_ErrorGetCode1: 0
BASS_MIDI_DLLCALL: 19529728
BASS_ErrorGetCode2: 0
BASS_Init: 1
BASS_ErrorGetCode3: 0
BASS_MIDI_HANDLE: 19529728
BASS_ErrorGetCode4: 0
File: C:\Program Files\Gramp\bass midi\test Beethoven's Fur Elise.mid
BASS_MIDI_StreamCreateFile: 0
BASS_ErrorGetCode5: 0
BASS_ChannelPlay: 0
BASS_ErrorGetCode6: 5

The only difference between his setup and mine seems to be winXP Sp2 for me and winXP Sp3 for him
Is there any possible issue with SP2?
(even if mp3s and miditest.exe work fine)
Do you think of something else that could be different or not working for me?
thx again for the support
« Last Edit: 27 Jun '11 - 22:20 by Lemo » Logged
Lemo
Posts: 11


« Reply #18 on: 6 Jul '11 - 02:51 »
Reply with quoteQuote

Okay.
It works.
:]

For some weird reason, this will only run without errors as a standalone compiled script in my case.
The usual process with Autohotkey is to finish the script as .ahk, then you can eventually
export it as .exe if you like, so people without Autohotkey may run it.
Actually I noticed that most of my previous tests scripts were working as well when "exported".
Well that's good news, but still kinda strange o_O
For example, the script I used for BASS mp3 works just fine as a simple .ahk script...

Anyway, thanks for the support
With that thread I now know a lot more about BASS, and the next steps are probably going to be a lot easier!

« Last Edit: 6 Jul '11 - 02:58 by Lemo » Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines