rtttl2tgml.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use SMS::Ringtone::RTTTL::Parser;
  2. our $NOTES = {
  3. 'A' =>[27.50, 55.00, 110.00, 220.00, 440.00, 880.00, 1760.00, 3520.00],
  4. 'A#' =>[29.14, 58.27, 116.54, 233.08, 466.16, 932.33, 1864.66, 3729.31],
  5. 'B' =>[30.87, 61.74, 123.47, 246.94, 493.88, 987.77, 1975.53, 3951.07],
  6. 'C' =>[16.35, 32.70, 65.41, 130.81, 261.63, 523.25, 1046.50, 2093.00, 4186.01],
  7. 'C#' =>[17.32, 34.65, 69.30, 138.59, 277.18, 554.37, 1108.73, 2217.46, 4434.92],
  8. 'D' =>[18.35, 36.71, 73.42, 146.83, 293.66, 587.33, 1174.66, 2349.32, 4698.64],
  9. 'D#' =>[19.45, 38.89, 77.78, 155.56, 311.13, 622.25, 1244.51, 2489.02, 4978.03],
  10. 'E' =>[20.60, 41.20, 82.41, 164.81, 329.63, 659.26, 1318.51, 2637.02],
  11. 'F' =>[21.83, 43.65, 87.31, 174.61, 349.23, 698.46, 1396.91, 2793.83],
  12. 'F#' =>[23.12, 46.25, 92.50, 185.00, 369.99, 739.99, 1479.98, 2959.96],
  13. 'G' =>[24.50, 49.00, 98.00, 196.00, 392.00, 783.99, 1567.98, 3135.96],
  14. 'G#' =>[25.96, 51.91, 103.83, 207.65, 415.30, 830.61, 1661.22, 3322.44],
  15. 'P' =>[0, 0, 0, 0, 0, 0, 0, 0]
  16. };
  17. my $file = shift or die "no file\n";
  18. my $rtttl;
  19. my $cr = "\n";
  20. if ($file eq "-nocr") {
  21. $cr = "";
  22. $file = shift;
  23. }
  24. if ($file eq "-") {
  25. $rtttl = <STDIN>;
  26. } else {
  27. $rtttl = `cat $file`;
  28. }
  29. $rtttl =~ s/\n//g;
  30. #$rtttl =~ tr/[a-z]/[A-Z]/;
  31. #print $rtttl;
  32. my $r = new SMS::Ringtone::RTTTL::Parser($rtttl);
  33. # ....or....
  34. #my $r = new SMS::Ringtone::RTTTL::Parser($rtttl,{'STRICT_NOTE_PART_ORDER' => 0});
  35. # Check for errors
  36. if ($r->has_errors()) {
  37. print STDERR "The following RTTTL errors were found:\n";
  38. foreach (@{$r->get_errors()}) {
  39. print STDERR "$_\n";
  40. }
  41. #exit;
  42. }
  43. # Dump parse results to STDOUT
  44. #$r->puke();
  45. $all = $r->get_notes();
  46. $ms_per_beat = int (6000 / $r->get_bpm());
  47. foreach (@{$all}) {
  48. my $ms = ($ms_per_beat * $_->[2]);
  49. print "%($ms,0,$NOTES->{$_->[1]}->[$_->[2]]);" . $cr;
  50. }