Code archives/Audio/saving midi file
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| saves a 64 lines midi file | |||||
;--------------------------------
; TRACKER STYLE MIDI FILE OUTPUT
;--------------------------------
; 2006 Bram32bit
; ver 3.01
;The MIDI file has a fixed size
;and writes only one pattern
;You cannot leave out notes without
;changing the size of the pattern
Dim Channel (127)
Dim Instrument(127)
;number of tracks
NumTracks = 2
;setup channels/instruments pro track
Channel (0) = 0 ;standard track
Instrument(0) = 8 ;celesta
Channel (1) = 9 ;drum track
Instrument(1) = 0 ;unused
;general tempo
BPM = 120
;-------------------------------------------------------------------------------------------------
; calculate tempo
;-------------------------------------------------------------------------------------------------
tt = 60000000 / BPM
t1 = (tt Shr 16)
t2 = (tt Shr 8) Mod 256
t3 = (tt Mod 256)
;opens file
Global MIDI_File = WriteFile("midifile.mid")
;-------------------------------------------------------------------------------------------------
; write MIDI header
;-------------------------------------------------------------------------------------------------
;[4] standard header
MIDI_WriteLine "MThd"
;[4] size header = 6 bytes
MIDI_WriteLine Chr$(0) + Chr$(0) + Chr$(0) + Chr$(6)
;[2] midi type 2
MIDI_WriteLine Chr$(0) + Chr$(2)
;[2] number of tracks
MIDI_WriteLine Chr$(0) + Chr$(3)
;[2] time base
MIDI_WriteLine Chr$($01) + Chr$($80)
;-------------------------------------------------------------------------------------------------
; tempo track
;-------------------------------------------------------------------------------------------------
;[4] track header
MIDI_WriteLine "MTrk"
;[4] track length in bytes = 10 bytes
MIDI_WriteLine Chr$(0) + Chr$(0) + Chr$(0) + Chr$($0A)
;[8] tempo
MIDI_WriteLine Chr$(0) + Chr$(255) + Chr$(81) + Chr$(3) + Chr$(t1) + Chr$(t2) + Chr$(t3) + Chr$(0)
;[3] end of track
MIDI_WriteLine Chr$(255) + Chr$(47) + Chr$(0)
For CurTrack = 0 To NumTracks - 1
;-------------------------------------------------------------------------------------------------
; track CurTrack
;-------------------------------------------------------------------------------------------------
;[4] track header
MIDI_WriteLine "MTrk"
;[4] track length in bytes
MIDI_WriteLine Chr$(0) + Chr$(0) + Chr$(2) + Chr$(7)
;[4] Channel: Program Change
MIDI_WriteLine Chr$(0) + Chr$($C0 + Channel(CurTrack)) + Chr$(Instrument(CurTrack)) + Chr$(0)
;-------------------------------------------------------------------------------------------------
; write notes
;-------------------------------------------------------------------------------------------------
For i = 0 To 63
note = Rand(128)
vol = Rand(128)
;[4] notes [status] [byte1] [byte2] [delta]
; on note volume time
Send $90 + Channel(CurTrack): Send note: Send $64: Send $5C
; off note volume time
Send $80 + Channel(CurTrack): Send note: Send $00: Send $04 * (i < 63)
Next
;-------------------------------------------------------------------------------------------------
; end of track
;-------------------------------------------------------------------------------------------------
;[3] end of track
MIDI_WriteLine Chr$(255) + Chr$(47) + Chr$(0)
Next
;closes file
CloseFile MIDI_File
;-------------------------------------------------------------------------------------------------
; testing MIDI
;-------------------------------------------------------------------------------------------------
;playback midi file
PlayMusic "midifile.mid"
WaitKey()
End
;-------------------------------------------------------------------------------------------------
; FUNCTIONS
;-------------------------------------------------------------------------------------------------
;--------------
;MIDI_WRITELINE
;--------------
;sends a string to the file
Function MIDI_WriteLine( t$ )
For i = 1 To Len( t$ )
WriteByte MIDI_File, Asc(Mid$(t$, i, 1))
Next
End Function
;-----
;SEND
;-----
;sends a byte to the file
Function Send( p )
WriteByte MIDI_File, p
End Function
;------------------------------------------------------------------------------------------------- |
Comments
| ||
| Very cool. I was interested in this but didn't have enough patient to learn it. Thanks. |
| ||
| lol music on the fly ...... |
| ||
| It runs normally on blitz, but does not work on windows media player. "Corrupted file!" How can I fix this? Gracias! |
| ||
| http://www.ccarh.org/courses/253/handout/smf/ |
| ||
| I'm sorry if this link has the answer to my question. But I could not find. I'm already studying midi for a while. But for me it is very difficult without an instructor. Therefore, I did not understand that the above information would have directly with the error correction detected in the execution of the file by windows. Suddenly I was not clear, since I am using a translator (Portuguese x English) from google. The code works on the blitz. It generates a file "midifile.mid". This file does not play on windows media player. Generates an error: "Corrupted file!". Anyone who had this code-generated file managed to run it on windows media player without any problems? I'm sorry, but my English is bad. So I do not know how to explain my doubt in a better way. |
| ||
Line 73 should readMIDI_WriteLine Chr$(0) + Chr$(0) + Chr$(0) + Chr$($0B) Make that change and the file will open in Media Player. |
| ||
| Perfect! It works on windows media player, and on my other players. Thank you very much for your great help. You do not know how happy I am with your simple but straightforward solution! I hope the translator has managed to convey my satisfaction. Once again, thank you! |
Code Archives Forum