Author Topic: Play a MIDI FIle with VB 2010  (Read 4927 times)

Genervd

  • Posts: 9
Play a MIDI FIle with VB 2010
« on: 17 Apr '12 - 09:36 »
Code: [Select]
Imports Un4seen.Bass
Imports Un4seen.Bass.AddOn.Midi

Public Class Form1
    Private Stream As Integer
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Me.Handle)
     
        Bass.BASS_PluginLoad("bassmidi.dll")
              BassMidi.BASS_MIDI_FontInit("C:\Users\Stephan\Desktop\brauchbar+\Giga Piano.sf2")
        Stream = BassMidi.BASS_MIDI_StreamCreateFile("C:\Users\Stephan\Documents\Harmonielehre\Harmonielehre\quintparallelen.mid", 0, 0, BASSFlag.BASS_DEFAULT, 44100)
        BassMidi.BASS_MIDI_StreamSetFonts(Stream, ?, ?)
        Bass.BASS_ChannelPlay(Stream, True)
    End Sub
End Class

I read the manuals from radio24 and un4seen, but I did not get any idea, what I have to write at this line
  BassMidi.BASS_MIDI_StreamSetFonts(Stream, ?, ?)

Ian @ un4seen

  • Administrator
  • Posts: 21741
Re: Play a MIDI FIle with VB 2010
« Reply #1 on: 17 Apr '12 - 16:36 »
You need to retain the BASS_MIDI_FontInit return value, and then use that (via a BASS_MIDI_FONT structure) in the BASS_MIDI_StreamSetFonts call. A VB.Net example snippet for that can be found in BASS.Net's BASS_MIDI_StreamSetFonts documentation. If you just want to use a single soundfont, then a simpler way to do it is via the BASS_CONFIG_MIDI_DEFFONT option, something like this...

Code: [Select]
Bass.BASS_SetConfigPtr(BASSConfig.BASS_CONFIG_MIDI_DEFFONT, "C:\Users\Stephan\Desktop\brauchbar+\Giga Piano.sf2")

Make sure BASSMIDI has been loaded before using that option, eg. by making a BASSMIDI function call during initialization.

Genervd

  • Posts: 9
Re: Play a MIDI FIle with VB 2010
« Reply #2 on: 18 Apr '12 - 08:19 »
Thanks, I had a problem of syntax.

Another question.

I thought, something like
Code: [Select]
BassMidi.BASS_MIDI_StreamEvent(Stream, 0, BASSMIDIEvent.MIDI_EVENT_NOTE, 98, 100)
would play a single note.

If I do

Code: [Select]
Stream = BassMidi.BASS_MIDI_StreamCreateFile("C:\Users\Stephan\Documents\Harmonielehre\Harmonielehre\dfg.mid", 0, 0, BASSFlag.BASS_DEFAULT, 44100)
Bass.BASS_ChannelPlay(Stream, True)
BassMidi.BASS_MIDI_StreamEvent(Stream, 0, BASSMIDIEvent.MIDI_EVENT_NOTE, 98, 100)

I hear a single note, but also the midifile.

If I do

Code: [Select]
Bass.BASS_ChannelPlay(Stream, True)
BassMidi.BASS_MIDI_StreamEvent(Stream, 0, BASSMIDIEvent.MIDI_EVENT_NOTE, 98, 100)

I hear nothing.

So I tried the first code with an MIDI-File without any note in it. I hear also nothing.

Is it possible to play a single note, or is it a missunderstanding of mine?





Genervd

  • Posts: 9
Re: Play a MIDI FIle with VB 2010
« Reply #3 on: 18 Apr '12 - 11:56 »
I was going on to understand bassmidi.dll

First I tried to change the tempo of a MIDI-File Loop

Code: [Select]
  Stream = BassMidi.BASS_MIDI_StreamCreateFile("C:\Users\Stephan\Downloads\output(29).mid", 0, 0, BASSFlag.BASS_SAMPLE_LOOP, 44100)
 Bass.BASS_ChannelPlay(Stream, True)
        BassMidi.BASS_MIDI_StreamEvent(Stream, 0, BASSMIDIEvent.MIDI_EVENT_TEMPO, 100000)


The fist time the midifile was played with the new tempo, but the other times the midifile was played with the original tempo.


So I tried this:
Code: [Select]
        Dim events(1) As BASS_MIDI_EVENT
        events(0).chan = 0
        events(0).eventtype = BASSMIDIEvent.MIDI_EVENT_TEMPO
        events(0).param = 50000
        events(0).tick = 2400

        BassMidi.BASS_MIDI_StreamEvents(Stream, BASSMIDIEventMode.BASS_MIDI_EVENTS_NONE, events)

Same result. But I discovered also, that the change of the tickvalue did not have any effect.

A further problem seems to me, that the change of tempo did not take place at the beginning of the midifile, but at the second quaver. Also when the tickvalue was 1 or 0. So the midifile is played in a musical wrong way. It is musical senseless.

What I have to do?

 

Ian @ un4seen

  • Administrator
  • Posts: 21741
Re: Play a MIDI FIle with VB 2010
« Reply #4 on: 18 Apr '12 - 15:29 »
Is it possible to play a single note, or is it a missunderstanding of mine?

Do you want to play a note without playing a MIDI file? If so, you can use BASS_MIDI_StreamCreate instead of BASS_MIDI_StreamCreateFile to create the MIDI stream.

First I tried to change the tempo of a MIDI-File Loop

Code: [Select]
  Stream = BassMidi.BASS_MIDI_StreamCreateFile("C:\Users\Stephan\Downloads\output(29).mid", 0, 0, BASSFlag.BASS_SAMPLE_LOOP, 44100)
 Bass.BASS_ChannelPlay(Stream, True)
        BassMidi.BASS_MIDI_StreamEvent(Stream, 0, BASSMIDIEvent.MIDI_EVENT_TEMPO, 100000)


The fist time the midifile was played with the new tempo, but the other times the midifile was played with the original tempo.

Yes, the MIDI file can override your events, eg. with its own tempo changes. In order to keep your changes, you can override the MIDI file's events via BASS_SYNC_MIDI_EVENT syncs. For example, to keep the tempo set to 100000, you could do this...

Code: [Select]
BASS_ChannelSetSync(stream, BASS_SYNC_MIDI_EVENT|BASS_SYNC_MIXTIME, MIDI_EVENT_TEMPO, TempoSyncProc, NULL); // set a "mixtime" sync on tempo events

...

void CALLBACK TempoSyncProc(HSYNC handle, DWORD channel, DWORD data, void *user)
{
BASS_MIDI_StreamEvent(channel, 0, MIDI_EVENT_TEMPO, 100000); // enforce the 100000 tempo setting
}

A more complete tempo changing demonstration can be found in the MIDITEST example in the BASSMIDI package.

But I discovered also, that the change of the tickvalue did not have any effect.

Yes, BASS_MIDI_StreamEvents applies the event(s) immediately (the "tick" and "pos" BASS_MIDI_EVENT members are ignored). If you would like to apply an event at a particular position, that can be achieved via a "mixtime" BASS_SYNC_POS or BASS_SYNC_MIDI_TICK sync, ie. set the sync at the wanted position and apply the event in your SYNCPROC callback function.

In case it is of interest, the next BASSMIDI release will include the option of providing a sequence of events for a MIDI stream to play. An update is also available to try now (BASS.Net already includes support for it)...

   www.un4seen.com/forum/?topic=13542.msg94618#msg94618

A further problem seems to me, that the change of tempo did not take place at the beginning of the midifile, but at the second quaver. Also when the tickvalue was 1 or 0. So the midifile is played in a musical wrong way. It is musical senseless.

What I have to do?

To have the tempo applied from the start, you will need to make the BASS_MIDI_StreamEvent call before you call BASS_ChannelPlay to begin playback. As above, you should also set a sync to enforce your tempo setting in case the MIDI file tries to set its own tempo.

Genervd

  • Posts: 9
Re: Play a MIDI FIle with VB 2010
« Reply #5 on: 18 Apr '12 - 23:33 »
I am trying to do the example at the end of http://www.un4seen.com/doc/bassmidi/BASS_MIDI_StreamEvents.html.

Is it correct, that it is not possible to do the same thing in Visual Basic 2010. I have tried
Code: [Select]
Dim events(2) As BASS_MIDI_EVENT
        events(0).chan = 0
        events(0).eventtype = BASSMIDIEvent.MIDI_EVENT_NOTE
        events(0).param = Un4seen.Bass.Utils.MakeWord(60, 127)

        events(1).chan = 0
        events(1).eventtype = BASSMIDIEvent.MIDI_EVENT_NOTE
        events(1).param = Un4seen.Bass.Utils.MakeWord(86, 126)
 BassMidi.BASS_MIDI_StreamEvents(Stream, AddOn.Midi.BASSMIDIEventMode.BASS_MIDI_EVENTS_SYNC, events)
Only the note of events(0) is played.



« Last Edit: 19 Apr '12 - 07:12 by Genervd »

radio42

  • Posts: 4647
Re: Play a MIDI FIle with VB 2010
« Reply #6 on: 18 Apr '12 - 23:38 »
The native BASS samples/docs are written for C/C++.
As you are coding in VB.NET you should use the Bass.Net documentation instead - which gives plenty of samples as well. See here: http://www.bass.radio42.com/help/Index.html


Genervd

  • Posts: 9
Re: Play a MIDI FIle with VB 2010
« Reply #7 on: 19 Apr '12 - 06:12 »
The native BASS samples/docs are written for C/C++.
As you are coding in VB.NET you should use the Bass.Net documentation instead - which gives plenty of samples as well. See here: http://www.bass.radio42.com/help/Index.html
Funny thing. I asked you yesterday in a email, after reading http://www.bass.radio42.com/help/Index.html, how I have to understand, how ticks work. You told me, I should ask the forum, because you have no idea, how it works. Now  is the suggested solution to read the documentation, which you can not explain.

Explain me, how I get out of your documentation, what I have to do. Test whether the documentaion explain really, what you thnink, it explain or it is a illusion of your own.

Explain, where is the information at http://www.bass.radio42.com/help/html/a6d45bac-5875-ddc5-2aad-9fbc4bed56f9.htm, which say, where I have to go using tick.

Try it really.

Auf Deutsch, ich würde mal die Dokumentation überprüfen, ob die wirklich so stringent ist, wie Du glaubst. Wenn die Dokumentation wirklich funktioniert, dann müsstest Du innerhalb von zwei Minuten erklären könnnen, wie das mit den ticks geht. Und zwar mit korrekter Syntax.







Ian @ un4seen

  • Administrator
  • Posts: 21741
Re: Play a MIDI FIle with VB 2010
« Reply #8 on: 19 Apr '12 - 14:34 »
Code: [Select]
Dim events(2) As BASS_MIDI_EVENT
        events(0).chan = 0
        events(0).eventtype = BASSMIDIEvent.MIDI_EVENT_NOTE
        events(0).param = Un4seen.Bass.Utils.MakeWord(60, 127)

        events(1).chan = 0
        events(1).eventtype = BASSMIDIEvent.MIDI_EVENT_NOTE
        events(1).param = Un4seen.Bass.Utils.MakeWord(86, 126)
 BassMidi.BASS_MIDI_StreamEvents(Stream, AddOn.Midi.BASSMIDIEventMode.BASS_MIDI_EVENTS_SYNC, events)
Only the note of events(0) is played.

I think you may be encountering a bug in the current BASSMIDI release which results in BASS_MIDI_StreamEvents only applying the 1st BASS_MIDI_EVENT element. Please try replacing your BASSMIDI.DLL file with this update and see if you still have the problem with it...

   www.un4seen.com/stuff/bassmidi.zip

Genervd

  • Posts: 9
Re: Play a MIDI FIle with VB 2010
« Reply #9 on: 25 Apr '12 - 08:18 »
Please try replacing your BASSMIDI.DLL file with this update and see if you still have the problem with it...

   www.un4seen.com/stuff/bassmidi.zip

Thanks it works now