#!/usr/bin/perl
print("Electronic Ringer ringtone generator\nby Dale Heatherington Feb 8, 2004\n");
@midi_header = (0x4d,0x54,0x68,0x64,0,0,0,6,0,0,0,1,0,0x80);
@midi_trackhdr = (0x4d,0x54,0x72,0x6b,0,0,0,0);
$low_note = 0x5f ; # low note  B5 = 1975hz
$high_note = 0x61 ; # high note C#6 = 2217hz
@time = (0xd); #note dwell time (50 ms)
$instr = 81;   #sets midi instrument
@name = (0,0xff,0x01,8,ord("E"),ord("-"),ord("R"),ord("i"),ord("n"),ord("g"),ord("e"),ord("r"));
@patch = (0x00,0xc0,$instr); #Program Change channel 1
@volume = (0,0xb0,7,0x7f);
@attack = (0,0xb0,0x49,0x3f);
@release = (0,0xb0,0x48,0x3f);
@end_track = (0,0xff,0x2f,00);
@low_note_on = (0,0x90,$low_note,0x7f);
@low_note_off = (@time,0x80,$low_note,0);
@high_note_on = (0,0x90,$high_note,0x7f);
@high_note_off = (@time,$high_note,0);
#
@track_setup = (@patch,@volume,@release,@attack);
@ring_cycle = (@low_note_on,@low_note_off,@high_note_on,@high_note_off);
@data = @track_setup;

$j = 0;
while($j < 5){
$i = 0;
while($i < 20){  #2 seconds of electronic ringer sound
	@data = (@data,@ring_cycle);
	$i = $i+1;
	}

@data = (@data,0x88,0x00,0xc0,$instr);  #4 seconds of silence
$j = $j+1;
}

#
@data = (@name,@data,@end_track); #Append end of track message
#
$tracksize = @data ;   #get size of track data
$midi_trackhdr[7] = $tracksize & 255; #Insert size into track header
$midi_trackhdr[6] = $tracksize / 256;

#print size to console for reference
print ("tracksize=",$tracksize,"\n");
@packet =  (@midi_header,@midi_trackhdr,@data);
#create a binary packet
$bin_str = $bin_str . pack("C*",@packet);
open( OUTFILE, ">E-Ringer.mid");
binmode(OUTFILE);
print OUTFILE ( $bin_str);  #write .mid file to disk
close(OUTFILE);
print "E-Ringer.mid file written. \n";




