Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Torpedo noises
#1
Here is a program that uses MIDI to simulate a torpedo coming at you and hitting you. The explosion kinda sucks, but oh well. It uses a modified version of keelings library. Here's the code:

Code:
'$include: 'midi.bi'

print "ahhhhh torpedo!"

dim hmidi as integer

hmidi = midiOpen



PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, 1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 15
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, 1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 10
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, 1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 5
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, 1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .9, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .8, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .7, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .6, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .5, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .4, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .3, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .2, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0

PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0
PlayNote hmidi, midiF, 7, .05, 5
PlayNote hmidi, midiFs, 7, .1, midiSynth_Voice
PlayNote hmidi, midiF, 7, .1, midiSeashore
sleep 0

print "boom"
PlayNote hmidi, midiF, 4, 0.5,midiGunshot
PlayNote hmidi, midiF, 7, .1, midiSeashore
PlayNote hmidi, midiF, 4, 2,midiHelicopter
PlayNote hmidi, midiF, 4, 4,midiSeashore

midiClose hmidi
'sleep

The new PlayNote takes a pointer to the midi device so that it doesn't have to open and close a device any time you want to play a note. Here's the new PlayNote code:
Code:
sub PlayNote (hMidi as integer, ByVal Note as integer, ByVal Octave as integer, _
   ByVal Duration as single, ByVal Instrument as integer = 0, _
   ByVal Volume as integer = 127)
  
   dim t as single
  
   MidiSetInstrument hMidi, Instrument
   MidiNoteOn hMidi,Note,Octave, Volume
  
   t = timer + Duration
   do while t > timer
      sleep 1
   loop
  
   MidiNoteOff hMidi, Note, Octave
end sub
.14159265358979323846264338327950288419716939937510582709445
Glarplesnarkleflibbertygibbertygarbethparkentalelelangathaffendoinkadonkeydingdonkaspamahedron.
Reply
#2
Cool,

This is a case where a good sound card with some nice soundfonts helps. :-)

I have given some thought about leaving the device open until closed and I still have mixed feelings about it. I like giving the programmer the option of 'setting and forgetting' the midi note, but I understand the problem of always openning and closing the device. Perhaps a second function. One function that opens and closes for you, and a second that assumes you are a big kid and will open and close the device yourself?

But what to call them?
Reply
#3
Code:
midiBegin(MIDI_INSTRUMENT_ NAME)    'add notes/ controls to midi statebuffer
   midiPlayNote (note, octave, duration ' add a note
midiEnd() ' end add to buffer

midiFlush ' play the notes
Also the PlayNote thing, it wasn't just that it was slow with the opening and closing of the device, it was the fact that it made all notes sticatto.
.14159265358979323846264338327950288419716939937510582709445
Glarplesnarkleflibbertygibbertygarbethparkentalelelangathaffendoinkadonkeydingdonkaspamahedron.
Reply
#4
Then why not keep the MidiPlayNote as is, and make it, MidiAddNote? And instead of Flush play the notes, why not a MidiPlayBuffer command so that you could revisit a buffer at will?

I will have to learn a bit about threads to make this work, but it seems like something that would be useful.

Just thinking out loud.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)