Noch 'ne alte Spielerei, um XProfan's Musikalität zu demonstrieren. :D;)
Diesmal, wie man MIDI-Dateien ohne MCISend$ abspielt, sondern selber parst und die richtigen API-Aufrufe zur richtigen Zeit tätigt.
Download als .exe-Datei: http://www.profchaos.mynetcologne.de/coding/MIDI-Visualizer.zip
Features:
+ Zeigt alle Tasten an, die gerade gedrückt sind, mit ihren jeweiligen Lautstärken (deren Verhältnis zueinander) -> Gelbanteil auf den individuellen Tasten.
+ Zeigt den Namen des Instruments an.
+ Zeigt die Tonhohenänderung innerhalb einer Note ("Pitch Bend") an -> linke Ziffer (0-16383).
+ Zeigt die Lautstärkenänderung innerhalb einer Note ("Expression") an -> rechte Ziffer (0-127).
+ Einzelne MIDI-Kanäle können durch Mausklick (in die entsprechende Zeile) sofort ein- bzw. ausgeschaltet werden.
+ Freie Auswahl des MIDI-Geräts (OnBoard-Soundkarte mit Verzögerung, Wavetable-Soundkarte ohne Verzögerung, Keyboard etc.)
Beschränkungen:
- Bildschirmauflösung sollte mindestens 1024x768 sein.
- RMI-Dateien funktionieren nicht (man müsste für sowas aber auch nur die ersten 20 Bytes der Datei überspringen).
- Ich habe nicht die leiseste Ahnung, wieviel Mhz das Ding wirklich braucht.
Als funktionierend getestet mit:
+ XProfan 9
+ FreeProfan32 (heute runtergeladen)
+ Windows Me, 1.8 Ghz
+ Windows Vista Home Premium 32 Bit, 2.8 Ghz
+ bisher noch jeder MID-Datei, die ich kenne! Nur halt keiner RMI-Datei.
Der Code könnte zugegeben besser dokumentiert und lesbarer sein. Ich hab zwar gerade nochmal drübergesehen, aber in den letzten 5 Jahren nichts weiter getan. ![]()
Das Ganze lässt sich bestimmt komplett auf den PLAY-Befehl umstellen - ohne direkte API-Aufrufe! - (Abstriche: vor allem Lautstärkenverhältnisse) und auf 4k kürzen (Abstriche: vor allem Ästhetik). Könnte ich mal was draus machen... bei Interesse. Aber nicht für den 4k-Wettbewerb. Für sowas würde ich eher was neues schreiben. Wenn ich das zeitlich hinkriege. Aber egal, ich schweife ab, hier der Quelltext zum Selber-sofort-kompilieren und -starten:
if (%maxx<1024) or (%maxy<768)
case messagebox("Dieses Programm ist für eine Bildschirmauflösung von 1024x768 optimiert.","Niedrige Auflösung",49)=2:end
endif
declare a#,a$,a&,b$,b%,c&,x%,y%,hmo&,siz&[1]
declare tpq%,tifac!,startime&,startick&,curtime&,curtick!
declare trknum%,trkcount%,trkleft%,trkstart&[99],trkaddr&[99],lastat%[99],nextick&[99]
declare keyxpos%[127],keycol%[127],keyshp%[127],velcol&[127,3]
declare chnlo%[15],chnhi%[15],chnuse%[127,15],chnprs%[127,15]
declare chnlst%,chnmute%[15],chnvol%[15]
def bigendianword(1) byte(a#,&(1))*256+byte(a#,&(1)+1)
def midiOutOpen(5) !"WINMM","midiOutOpen"
def midiOutShortMsg(2) !"WINMM","midiOutShortMsg"
def midiOutClose(1) !"WINMM","midiOutClose"
def midiOutGetNumDevs(0) !"WINMM","midiOutGetNumDevs"
def midiOutGetDevCaps(3) !"WINMM","midiOutGetDevCapsA"
def GetCurrentThread(0) !"KERNEL32","GetCurrentThread"
def SetThreadPriority(2) !"KERNEL32","SetThreadPriority"
def GetTextExtentPoint(4) !"GDI32","GetTextExtentPoint32A"
def GetRelativeFileName$(1) substr$($(1),len($(1))-len(translate$($(1),"\",""))+1,"\")
def gradient(3) rgb((getrvalue(&(2))-getrvalue(&(1)))*!(3)+getrvalue(&(1)),\
(getgvalue(&(2))-getgvalue(&(1)))*!(3)+getgvalue(&(1)),\
(getbvalue(&(2))-getbvalue(&(1)))*!(3)+getbvalue(&(1)))
dim$ 255
list$ 0="Acou.Grand"
list$ 1="Bright Acou."
list$ 2="Elec.Grand"
list$ 3="Honky Tonk"
list$ 4="E.Piano 1"
list$ 5="E.Piano 2"
list$ 6="Harpsichord"
list$ 7="Clav"
list$ 8="Celesta"
list$ 9="Glockensp."
list$ 10="Music Box"
list$ 11="Vibraphone"
list$ 12="Marimba"
list$ 13="Xylophone"
list$ 14="TubularBells"
list$ 15="Dulcimer"
list$ 16="Draw.Organ"
list$ 17="Perc.Organ"
list$ 18="Rock Organ"
list$ 19="Chur.Organ"
list$ 20="Reed Organ"
list$ 21="Accordion"
list$ 22="Harmonica"
list$ 23="Bandoneon"
list$ 24="Nylon Guitar"
list$ 25="Steel Guitar"
list$ 26="Jazz Guitar"
list$ 27="Clean Guitar"
list$ 28="GuitarMutes"
list$ 29="Ovrdriv.Guit."
list$ 30="Distort.Guit."
list$ 31="Harmonics"
list$ 32="Acou.Bass"
list$ 33="Finger.Bass"
list$ 34="Picked Bass"
list$ 35="Fretles.Bass"
list$ 36="Slap Bass 1"
list$ 37="Slap Bass 2"
list$ 38="SynthBass 1"
list$ 39="SynthBass 2"
list$ 40="Violin"
list$ 41="Viola"
list$ 42="Cello"
list$ 43="Contrabass"
list$ 44="Tremolo Str."
list$ 45="Pizzicato"
list$ 46="Harp"
list$ 47="Timpani"
list$ 48="Strings"
list$ 49="Slow Strings"
list$ 50="Synth Str.1"
list$ 51="Synth Str.2"
list$ 52="Choir Aahs"
list$ 53="Voice Oohs"
list$ 54="Synth Vox"
list$ 55="Orch.Hit"
list$ 56="Trumpet"
list$ 57="Trombone"
list$ 58="Tuba"
list$ 59="Mute Trmpt."
list$ 60="French Horn"
list$ 61="Brass Sect."
list$ 62="SynthBrass1"
list$ 63="SynthBrass2"
list$ 64="SopranoSax"
list$ 65="Alto Sax"
list$ 66="Tenor Sax"
list$ 67="BaritoneSax"
list$ 68="Oboe"
list$ 69="EnglishHorn"
list$ 70="Bassoon"
list$ 71="Clarinet"
list$ 72="Piccolo"
list$ 73="Flute"
list$ 74="Recorder"
list$ 75="Pan Flute"
list$ 76="BlownBottle"
list$ 77="Shakuhachi"
list$ 78="Whistle"
list$ 79="Ocarina"
list$ 80="SquareWave"
list$ 81="Saw Wave"
list$ 82="Syn.Calliope"
list$ 83="Chiffer Lead"
list$ 84="Charang"
list$ 85="Solo Vox"
list$ 86="5th Saw Wv."
list$ 87="Bass & Lead"
list$ 88="Fantasia"
list$ 89="Warm Pad"
list$ 90="Poly Synth"
list$ 91="Space Voice"
list$ 92="BowedGlass"
list$ 93="Metal Pad"
list$ 94="Halo Pad"
list$ 95="Sweep Pad"
list$ 96="Ice Rain"
list$ 97="Soundtrack"
list$ 98="Crystal"
list$ 99="Atmosphere"
list$ 100="Brightness"
list$ 101="Goblin"
list$ 102="Echo Drops"
list$ 103="Star Theme"
list$ 104="Sitar"
list$ 105="Banjo"
list$ 106="Shamisen"
list$ 107="Koto"
list$ 108="Kalimba"
list$ 109="Bagpipe"
list$ 110="Fiddle"
list$ 111="Shanai"
list$ 112="Tinkle Bell"
list$ 113="Agogo"
list$ 114="Steel Drums"
list$ 115="Woodblock"
list$ 116="Taiko Drum"
list$ 117="MelodicTom"
list$ 118="Synth Drum"
list$ 119="RevrsCymbl."
list$ 120="Fret Noise"
list$ 121="BreathNoise"
list$ 122="Seashore"
list$ 123="Bird"
list$ 124="Telephone"
list$ 125="Helicopter"
list$ 126="Applause"
list$ 127="Gunshot"
whileloop 128,255
list$ &loop="Undefined"
endwhile
list$ 128="Standard"
list$ 136="Room"
list$ 144="Power"
list$ 152="Electronic"
list$ 153="TR-808"
list$ 160="Jazz"
list$ 168="Brush"
list$ 176="Orchestra"
list$ 184="SFX"
list$ 255="CM-64/32L"
proc die
whileloop $78B0,$78BF
midiOutShortMsg(hmo&,&loop)
endwhile
midiOutClose(hmo&)
dispose a#
end
endproc
proc fillkey
parameters note%,chn%,vel%,used%
usebrush 1,velcol&[vel%,setbit(keycol%[note%],1,used%)]
fill keyxpos%[note%],(chn%*45)+1,0
endproc
proc drawkey
parameters note%,chn%
declare b%,x%,y%
x%=keyxpos%[note%]
y%=chn%*45
b%=keyshp%[note%]
if b%=0
moveto x%-6,y%
lineto x%+3,y%
lineto x%+3,y%+25
lineto x%+6,y%+25
lineto x%+6,y%+45
lineto x%-6,y%+45
lineto x%-6,y%
elseif b%=1
moveto x%-3,y%
lineto x%+3,y%
lineto x%+3,y%+25
lineto x%+6,y%+25
lineto x%+6,y%+45
lineto x%-6,y%+45
lineto x%-6,y%+25
lineto x%-3,y%+25
lineto x%-3,y%
elseif b%=2
moveto x%-3,y%
lineto x%+6,y%
lineto x%+6,y%+45
lineto x%-6,y%+45
lineto x%-6,y%+25
lineto x%-3,y%+25
lineto x%-3,y%
elseif b%=3
rectangle x%-3,y%-x%+4,y%+26
endif
endproc
proc paintkey
parameters note%,chn%,vel%,used%
usebrush 0,0
drawkey note%,chn%
fillkey note%,chn%,vel%,used%
endproc
proc readtime
c&=0
b%=byte(a#,a&)
while testbit(b%,7)
c&=(c&+(b% mod 128))*128
inc a&
b%=byte(a#,a&)
endwhile
add c&,b%
inc a&
endproc
' Tasten definieren
whileloop 0,127
b%=(&loop mod 12)+1
keyxpos%[&loop]=val(mid$("654327654321",b%,1))+&loop*7
b%=val(mid$("031320313132",b%,1))
keyshp%[&loop]=b%
keycol%[&loop]=b%=3
velcol&[&loop,0]=$EEEEEE
velcol&[&loop,1]=$555555
velcol&[&loop,2]=gradient($FFFFFF,$00CCCC,&loop/127)
velcol&[&loop,3]=gradient($333333,$00CCCC,&loop/127)
endwhile
keyshp%[127]=2
' MIDI-Ausgabegeräte auflisten
dim a#,52
whileloop 0,midiOutGetNumDevs()-1
midiOutGetDevCaps(&loop,a#,52)
addstring char$(a#,8,32)
endwhile
dispose a#
' Benutzereingaben
decimals 0
windowtitle "MIDI-Visualizer 0.95 © 2007-8 Sebastian Sprenger <http://www.profchaos.mynetcologne.de>"
windowstyle $1A
window 0,0-%maxx,%maxy
settruecolor 1
cls $AAAAAA
'ifnot %parcount
a$=loadfile$("MIDI-Datei profan abspielen","MIDI-Dateien|*.MID")
'else
' a$=par$(1)
'endif
case findfirst$(a$)="":end
if %getcount
listbox$("MIDI-Ausgabegeräte",1)
case %button=2:end
endif
' MIDI-Datei laden, erkennen und MIDI-Ausgabegerät öffnen
filemode 0
assign #1,a$
openrw #1
a&=getfilesize(#1)
dim a#,a&
blockread(#1,a#,0,a&)
closerw #1
midiOutOpen(addr(hmo&),%getcursel,0,0,0)
casenot char$(a#,0,4)="MThd":die
SetThreadPriority(GetCurrentThread(),1)
' Titel aus Dateinamen ableiten
a$=GetRelativeFileName$(a$)
if len(a$)>4
b$=upper$(right$(a$,4))
case (b$=".MID") or (b$=".RMI"):a$=left$(a$,len(a$)-4)
endif
' und in den Hintergrund
y%=0
orientation 450
repeat
inc y%
usefont "Arial Black",y%,0,0,0,0
GetTextExtentPoint(%hdc,addr(a$),len(a$),addr(siz&[0]))
until (siz&[0]*sin(pi()*.25)+siz&[1])>720
usefont "Arial Black",y%-1,0,0,0,0
textcolor $BBBBBB,-1
drawtext 0,720-siz&[1],a$
' Inaktive Klaviaturen zeichnen
usepen 0,1,$999999
usebrush 1,$999999
whileloop 0,15
b%=&loop
whileloop 0,127
drawkey &loop,b%
endwhile
chnlo%[&loop]=128
chnhi%[&loop]=-1
chnvol%[&loop]=100
endwhile
' MIDI-Datei einlesen und überprüfen
usepen 0,1,0
trkcount%=bigendianword(10)
tpq%=bigendianword(12)
a&=8
whileloop 0,trkcount%-1
add a&,8+bigendianword(a&-4)*65536+bigendianword(a&-2)
casenot char$(a#,a&-8,4)="MTrk":die
trkstart&[&loop]=a&
readtime
y%=256
whilenot word(a#,a&)=$2FFF
b%=byte(a#,a&)
if b%<128
b%=y%
dec a&
elseif b%<240
y%=b%
endif
if b%<192
if b%<160
x%=byte(a#,a&+1)
b%=b% & 15
if x%<chnlo%[b%]
if chnlo%[b%]<128
whileloop x%+1,chnlo%[b%]-1
paintkey &loop,b%,0,0
endwhile
endif
chnlo%[b%]=x%
endif
if x%>chnhi%[b%]
if chnhi%[b%]>=0
whileloop chnhi%[b%]+1,x%-1
paintkey &loop,b%,0,0
endwhile
endif
chnhi%[b%]=x%
endif
ifnot chnuse%[x%,b%]
chnuse%[x%,b%]=1
paintkey x%,b%,0,1
endif
endif
add a&,3
elseif b%<224
add a&,2
elseif b%<240
add a&,3
elseif b%=240
add a&,2+byte(a#,a&+1)
elseif b%=255
add a&,3+byte(a#,a&+2)
else
die
endif
readtime
endwhile
a&=trkstart&[&loop]
endwhile
orientation 0
usefont "Arial Black",24,0,0,0,0
textcolor 0,$AAAAAA
chnlst%=-1
whilenot scankey(27)
' Stück ggf. neustarten
ifnot trkleft%
trkleft%=trkcount%
whileloop 0,trkcount%-1
a&=trkstart&[&loop]
readtime
if word(a#,a&)=$2FFF ' Spur schon zu Ende?
a&=0
dec trkleft%
endif
trkaddr&[&loop]=a&
nextick&[&loop]=c& ' Zeit bis zum ersten Ereignis lesen
endwhile
' Timing berechnen und starten
tifac!=tpq%/500
startick&=0
startime&=&gettickcount
usepen 0,1,$AAAAAA
usebrush 1,$AAAAAA
rectangle 903,0-width(%hwnd),height(%hwnd)
endif
' Aktuelle Position berechnen
curtime&=&gettickcount
curtick!=(curtime&-startime&)*tifac!+startick&
' Stück weiterspielen
whileloop 0,trkcount%-1
a&=trkaddr&[&loop]
casenot a&:continue
trknum%=&loop
whilenot nextick&[trknum%]>curtick!
b%=byte(a#,a&)
if b%<128
midiOutShortMsg(hmo&,word(a#,a&)*256+lastat%[trknum%])
b%=lastat%[trknum%]
dec a&
elseif b%<240
midiOutShortMsg(hmo&,long(a#,a&))
lastat%[trknum%]=b%
endif
y%=b% & 15
if b%<192
if b%<160
x%=byte(a#,a&+1)
b%=(b%>143)*byte(a#,a&+2)
chnprs%[x%,y%]=b%
casenot chnmute%[y%]:fillkey x%,y%,b%,1
elseif (b%>=176)
b%=byte(a#,a&+1)
x%=byte(a#,a&+2)
if b%=7
chnvol%[y%]=x%
case chnmute%[y%]:midiOutShortMsg(hmo&,$07B0+y%)
elseif b%=11
a$=str$(x%)
drawtext 981,y%*45+22,a$+space$((3-len(a$))*2)
endif
endif
add a&,3
elseif b%<224
case b%<208:drawtext 903,y%*45,list$(setbit(byte(a#,a&+1),7,b%=201))+space$(20)
add a&,2
elseif b%<240
a$=str$(byte(a#,a&+1)+128*byte(a#,a&+2))
drawtext 903,y%*45+22,a$+space$((5-len(a$))*2)
add a&,3
elseif b%=240
add a&,2+byte(a#,a&+1)
elseif b%=255
if byte(a#,a&+1)=81 ' Tempo
startime&=curtime&
startick&=nextick&[trknum%]
tifac!=tpq%/((byte(a#,a&+3)*65536+byte(a#,a&+4)*256+byte(a#,a&+5))/1000)
endif
add a&,3+byte(a#,a&+2)
endif
readtime
nextick&[trknum%]=nextick&[trknum%]+c& ' Zeit bis zum nächsten Ereignis
casenot word(a#,a&)=$2FFF:continue ' Spur zu Ende?
a&=0
dec trkleft%
break
endwhile
trkaddr&[trknum%]=a&
endwhile
x%=%mousepressed
y%=%mousey\45
if not(x%) or (y%<0) or (y%>=16)
chnlst%=-1
elseif y%<>chnlst%
chnmute%[y%]=1-chnmute%[y%]
a&=$07B0+y%
if chnmute%[y%]
whileloop chnlo%[y%],chnhi%[y%]
fillkey &loop,y%,chnprs%[&loop,y%],0
endwhile
else
whileloop chnlo%[y%],chnhi%[y%]
fillkey &loop,y%,chnprs%[&loop,y%],chnuse%[&loop,y%]
endwhile
add a&,chnvol%[y%] << 16
endif
midiOutShortMsg(hmo&,a&)
chnlst%=y%
endif
endwhile
die
Alles anzeigen