monoteen

SystemBeep_Tetris

Oct 7th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace MusicBeeper
  8. {
  9.     public class Tones
  10.     {
  11.         public enum Octave {
  12.             DoublePedal,
  13.             Pedal,
  14.             Deep,
  15.             Low,
  16.             Middle,
  17.             Tenor,
  18.             High,
  19.             DoubleHigh,
  20.             Size
  21.         };
  22.  
  23.         public enum Duration
  24.         {
  25.             Whole = 1000,
  26.             Half = 500,
  27.             Quarter = 250,
  28.             Eighth = 125,
  29.             Sixteenth = 62
  30.         };
  31.  
  32.         public enum Note
  33.         {
  34.             C, Cs, D, Ds, E, F, Fs, G, Gs, A, As, B, Size
  35.         };
  36.  
  37.         public static int[] Frequency = {
  38.                                             16,17,18,19,21,22,23,24,26,27,29,31,
  39.                                             33,35,37,39,41,44,46,49,52,55,58,62,
  40.                                             65,69,73,78,82,87,92,98,104,110,116,123,
  41.                                             131,139,147,155,165,175,185,196,208,220,233,245,
  42.                                             262,277,294,311,330,349,370,392,415,440,466,494,
  43.                                             523,554,587,622,659,698,740,784,831,880,932,988,
  44.                                             1046,1109,1175,1244,1328,1397,1480,1568,1661,1760,1865,1975,
  45.                                             2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951
  46.                                         };
  47.         public static void PlaySong( string song )
  48.         {
  49.             string[] tokens = song.Split( ',' );
  50.  
  51.             int freq, dur;
  52.             foreach ( string token in tokens )
  53.             {
  54.                 Parse( token, out freq, out dur );
  55.                 Beep.Play( freq, dur );
  56.             }
  57.         }
  58.  
  59.         public static void PlayNote( Note note )
  60.         {
  61.             PlayNote( note, Octave.Middle, Duration.Quarter );
  62.         }
  63.  
  64.         public static void PlayNote( Note note, Octave oct )
  65.         {
  66.             PlayNote( note, oct, Duration.Quarter );
  67.         }
  68.  
  69.         public static void PlayNote( Note note, Duration dur )
  70.         {
  71.             PlayNote( note, Octave.Middle, dur );
  72.         }
  73.  
  74.         public static void PlayNote( Note note, Octave oct, Duration dur )
  75.         {
  76.             int freq = GetFreq( note, oct );
  77.             Beep.Play( freq, (int)dur );
  78.         }
  79.  
  80.         public static int GetFreq( Note note, Octave oct )
  81.         {
  82.             return GetFreq( (int)note, (int)oct );
  83.         }
  84.  
  85.         public static int GetFreq( Note note, int oct )
  86.         {
  87.             if ( oct < 0 || oct >= (int)Octave.Size )
  88.                 throw new ArgumentOutOfRangeException("oct", "Octave value exceeds range.");
  89.  
  90.             return GetFreq( (int)note, oct );
  91.         }
  92.  
  93.         private static int GetFreq( int note, int oct )
  94.         {
  95.             return Frequency[(int)Note.Size * oct + note];
  96.         }
  97.  
  98.         public static void Parse( string line, out int freq, out int dur )
  99.         {
  100.             freq = 0;
  101.             dur = (int)Duration.Quarter;
  102.             int octIdx = (int)Octave.Middle;
  103.  
  104.  
  105.             string[] tokens = line.Split( '-' );
  106.  
  107.             int noteIdx = ParseNote( tokens.First().ToUpper().Trim() );
  108.            
  109.             if ( tokens.Length >= 3 )
  110.             {
  111.                 octIdx = ParseOctive( tokens[1] );
  112.             }
  113.  
  114.             if ( tokens.Length >= 2 )
  115.             {
  116.                 dur = ParseDuration( tokens.Last() );
  117.             }
  118.  
  119.             if ( tokens.First().ToUpper().Trim() == "P" )
  120.             {
  121.                 freq = 22000;
  122.                 Console.WriteLine( string.Format("일시정지: {0}", (Duration)dur ) );
  123.             }
  124.             else
  125.             {
  126.                 freq = GetFreq( noteIdx, octIdx );
  127.                 Console.WriteLine( string.Format( "옥타브: {0} 노트: {1}", octIdx, (Note)noteIdx ) );
  128.             }
  129.         }
  130.  
  131.         private static int ParseDuration( string strValue )
  132.         {
  133.             float step;
  134.             int milliseconds;
  135.  
  136.             if ( !float.TryParse( strValue, out step ) )
  137.                 step = (float)Duration.Whole;
  138.  
  139.             switch ( (int)step )
  140.             {
  141.                 case 1:
  142.                     milliseconds = (int)Duration.Whole;
  143.                     break;
  144.                 case 2:
  145.                     milliseconds = (int)Duration.Half;
  146.                     break;
  147.                 case 4:
  148.                     milliseconds = (int)Duration.Quarter;
  149.                     break;
  150.                 case 8:
  151.                     milliseconds = (int)Duration.Eighth;
  152.                     break;
  153.                 case 16:
  154.                     milliseconds = (int)Duration.Sixteenth;
  155.                     break;
  156.                 default:
  157.                     milliseconds = (int)Duration.Whole;
  158.                     break;
  159.             }
  160.  
  161.             if ( step - (int)step > 0.0 )
  162.                 milliseconds += ( milliseconds / 2 );
  163.  
  164.             return milliseconds;
  165.         }
  166.  
  167.         private static int ParseOctive( string strValue )
  168.         {
  169.             int octIdx;
  170.  
  171.             if ( !int.TryParse( strValue, out octIdx ) )
  172.                 octIdx = (int)Octave.Middle;
  173.  
  174.             return octIdx;
  175.         }
  176.  
  177.         private static int ParseNote( string strValue )
  178.         {
  179.             int noteIdx;
  180.  
  181.             switch ( strValue )
  182.             {
  183.                 case "C":
  184.                     noteIdx = (int)Note.C;
  185.                     break;
  186.                 case "CS":
  187.                     noteIdx = (int)Note.Cs;
  188.                     break;
  189.                 case "D":
  190.                     noteIdx = (int)Note.D;
  191.                     break;
  192.                 case "DS":
  193.                     noteIdx = (int)Note.Ds;
  194.                     break;
  195.                 case "E":
  196.                     noteIdx = (int)Note.E;
  197.                     break;
  198.                 case "F":
  199.                     noteIdx = (int)Note.F;
  200.                     break;
  201.                 case "FS":
  202.                     noteIdx = (int)Note.Fs;
  203.                     break;
  204.                 case "G":
  205.                     noteIdx = (int)Note.G;
  206.                     break;
  207.                 case "GS":
  208.                     noteIdx = (int)Note.Gs;
  209.                     break;
  210.                 case "A":
  211.                     noteIdx = (int)Note.A;
  212.                     break;
  213.                 case "AS":
  214.                     noteIdx = (int)Note.As;
  215.                     break;
  216.                 case "B":
  217.                     noteIdx = (int)Note.B;
  218.                     break;
  219.                 default:
  220.                     noteIdx = (int)Note.C;
  221.                     break;
  222.             }
  223.  
  224.             return noteIdx;
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment