Advertisement
Guest User

Untitled

a guest
May 4th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1.  
  2. # Convert the note on/off events to frequency/duration pair
  3. # 1. Create the midi notes2frequency table. Tuning is based upon A=440
  4. my $a = 440; # a is 440 hz...
  5. my @midi;
  6. for($x = 0; $x < 127; ++$x)
  7. {
  8. $midi[$x] = ($a / 32) * (2 ** (($x - 9) / 12));
  9. }
  10. # 2. Parse the channel events
  11. my $note_on = 0;
  12. my $note_on_time = 0;
  13. my $note = 0;
  14. my $velocity = 0;
  15. my $end_time = 0;
  16. print("//{frequency, volume, duration}\n");
  17. while ($a = <>) {
  18. if (!($a =~ m/\s*[\#\;]/)) { # Ignore comment lines
  19. # match note on
  20. if ($a =~ m/\s*\d+\s*,\s*(\d+)\s*,\s*Note_on_c\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*/) {
  21. # $1 time, $2 channel, $3 note, $4 velocity
  22. if(!$note_on) {
  23. $note_on = 1;
  24. } else {
  25. print("{".$midi[$note].",".$velocity.",".($1 - $note_on_time)."}\n");
  26. }
  27. $note_on_time = $1;
  28. $note = $3;
  29. $velocity = $4;
  30. }
  31. # match note off
  32. elsif ($a =~ m/\s*\d+\s*,\s*\d+\s*,\s*Note_off_c\s*,\s*(\d+)/){
  33. if($note_on) {
  34. $note_on = 0;
  35. print("{".$midi[$note].",".$velocity.",".($1 - $note_on_time)."}\n");
  36. }
  37. }
  38. # match end of track
  39. elsif ($a =~ m/\s*\d+\s*,\s*(\d+)\s*,\s*End_track\s*/){
  40. $end_time = $1;
  41. }
  42. }
  43. }
  44. # The last note may not be set off
  45. if($note_on) {
  46. print("{".$midi[$note].",".$velocity.",".($end_time - $note_on_time)."}\n");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement