c# - Midi-dot-net sounds played twice -


firstable not pro programmer student hava little problem excelent library midi-dot-net.

i use midi-dot-net library write simple application in visual studio in c# language. stuck on confusing problem.

i placed little code form1.cs

public void noteon(noteonmessage msg) {     if (invokerequired) {         begininvoke(noteonhandler, msg);         return;     }       inputstatuslabel.text = string.format("{0}", msg.pitch);      string nutka = inputstatuslabel.text;      if (nutka == "a0") {         clock.schedule(new noteonmessage(outputdevice, channel.channel1, pitch.a0, 80, 2));     } } 

so, placed new string , called nutka, , nutka receive name of pressed note on usb midi keyboard. placed if statment, , compared nutka "a0" (first note on keyboard).

then if it's "a0" pressed outputdevice plays specific note. , plays plays twice, 1 - when press key (with note a0) on keyboard , second time - when release key.

i did breakpoint on public void noteon(noteonmessage msg) , noticed application returns here twice , plays twice note, still don't know why.

one more thing, there method public void noteoff(noteoffmessage message), seems not working.

i can't figure out, , i'm looking help.

update ... update ... update

another problem appears (the first part resolved cl , chris dunaway advice , step-by-step justin explanation).

thank justin :) see life without problems not possible :)

with clock.schedule can play midi sound want play example piano notes (wav file format) , in 4-5 weeks college me recording own piano sounds every note. want play them simultaneous.

i thought fine , now... got problem simultaneous playing. trying check 3 possibilities:

1) trying play piano sounds basic notes library have , use soundplayer it:

soundplayer notea0 = new soundplayer(properties.resources.a0); notea0.play(); 

i use every note statement different soundplayer name (depend on note name) , noticed - can't play notes simultaneously.

2) used next wmp library , windowsmediaplayer:

for example:

var notea0 = new wmplib.windowsmediaplayer(); notea0.url = @"c:\sounds\piano\a0.wav"; 

ok... plays simultaneous looks more notes play or play song bigger latency got , finnaly programm stuck on playing anything...

3) trying use microsoft.directx.audiovideoplayback:

audio notea1 = new audio(@"c:\sounds\piano\a1.wav"); notea1.play(); 

i started programm, pressed key , boom! crashed error message:

an unhandled exception of type 'system.badimageformatexception' occurred in midi.dll additional information: not load file or assembly 'microsoft.directx.audiovideoplayback.dll' or 1 of dependencies.  not valid win32 application. (exception hresult: 0x800700c1) 

of course didn't forget use:

using system.media; using microsoft.directx.audiovideoplayback; using microsoft.directx; 

right have no idea can more , again - need of experienced person :)

expanding on cl.'s answer, here sequence of events probably happening:

  1. you push key on midi keyboard sends out note on message, pitch of a0, , velocity (the exact velocity may vary every time).
  2. your code receives and, since matches if statement, sends out note on message, pitch of a0, , velocity of 80.

here's gets tricky. midi devices send corresponding note off message same pitch when release key. other devices send second note on message same pitch, velocity of 0. these 2 types of message (or should be) functionally equivalent, , each device can either stop note playing (or both). so,

  1. you release key on keyboard sends out note on message, pitch of a0, , velocity of 0.
  2. your code receives and, since matches if statement, sends out note on message, pich of a0, , velocity of 80.

since note on velocity of 0 note off, happens code turning keyboard's "note off" note on, playing sound twice.

as chris dunaway suggested, need determine if note on "note off" testing velocity of 0.

    if (nutka == "a0" && msg.velocity != 0) {         clock.schedule(new noteonmessage(outputdevice, channel.channel1, pitch.a0, 80, 2));     } 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -