■ SoundPlayer 클래스의 PlaySync 메소드를 사용해 비프음을 만드는 방법을 보여준다.
▶ SoundPlayer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
using System.Media; namespace TestProject { /// <summary> /// 사운드 헬퍼 /// </summary> public static class SoundHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field // C public const int C0 = 16; public const int C1 = 33; public const int C2 = 65; public const int C3 = 131; public const int C4 = 262; public const int C5 = 523; public const int C6 = 1046; public const int C7 = 2093; // C# public const int CS0 = 17; public const int CS1 = 35; public const int CS2 = 69; public const int CS3 = 139; public const int CS4 = 277; public const int CS5 = 554; public const int CS6 = 1109; public const int CS7 = 2217; // D public const int D0 = 18; public const int D1 = 37; public const int D2 = 73; public const int D3 = 147; public const int D4 = 294; public const int D5 = 587; public const int D6 = 1175; public const int D7 = 2349; // D# public const int DS0 = 19; public const int DS1 = 39; public const int DS2 = 78; public const int DS3 = 155; public const int DS4 = 311; public const int DS5 = 622; public const int DS6 = 1244; public const int DS7 = 2489; // E public const int E0 = 21; public const int E1 = 41; public const int E2 = 82; public const int E3 = 165; public const int E4 = 330; public const int E5 = 659; public const int E6 = 1328; public const int E7 = 2637; // F public const int F0 = 22; public const int F1 = 44; public const int F2 = 87; public const int F3 = 175; public const int F4 = 349; public const int F5 = 698; public const int F6 = 1397; public const int F7 = 2794; // F# public const int FS0 = 23; public const int FS1 = 46; public const int FS2 = 92; public const int FS3 = 185; public const int FS4 = 370; public const int FS5 = 740; public const int FS6 = 1480; public const int FS7 = 2960; // G public const int G0 = 24; public const int G1 = 49; public const int G2 = 98; public const int G3 = 196; public const int G4 = 392; public const int G5 = 784; public const int G6 = 1568; public const int G7 = 3136; // G# public const int GS0 = 26; public const int GS1 = 52; public const int GS2 = 104; public const int GS3 = 208; public const int GS4 = 415; public const int GS5 = 831; public const int GS6 = 1661; public const int GS7 = 3322; // A public const int A0 = 27; public const int A1 = 55; public const int A2 = 110; public const int A3 = 220; public const int A4 = 440; public const int A5 = 880; public const int A6 = 1760; public const int A7 = 3520; // A# public const int AS0 = 29; public const int AS1 = 58; public const int AS2 = 116; public const int AS3 = 233; public const int AS4 = 466; public const int AS5 = 932; public const int AS6 = 1865; public const int AS7 = 3729; // B public const int B0 = 31; public const int B1 = 62; public const int B2 = 123; public const int B3 = 245; public const int B4 = 494; public const int B5 = 988; public const int B6 = 1975; public const int B7 = 3951; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 비프음 발생시키기 - Beep(amplitude, frequency, duration) /// <summary> /// 비프음 발생시키기 /// </summary> /// <param name="amplitude">진폭</param> /// <param name="frequency">주파수</param> /// <param name="duration">지속 시간</param> public static void Beep(int amplitude, int frequency, int duration) { double a = ((amplitude * (Math.Pow(2, 15))) / 1000) - 1; double deltaFT = 2 * Math.PI * frequency / 44100.0; int sampleCount = 441 * duration / 10; int byteCount = sampleCount * 4; int[] hdrArray = new int[] { 0x46464952, 36 + byteCount, 0x45564157, 0x20746D66, 16, 0x20001, 44100, 176400, 0x100004, 0x61746164, byteCount }; using(MemoryStream stream = new MemoryStream(44 + byteCount)) { using(BinaryWriter writer = new BinaryWriter(stream)) { for(int i = 0; i < hdrArray.Length; i++) { writer.Write(hdrArray[i]); } for(int t = 0; t < sampleCount; t++) { short Sample = Convert.ToInt16(a * Math.Sin(deltaFT * t)); writer.Write(Sample); writer.Write(Sample); } writer.Flush(); stream.Seek(0, SeekOrigin.Begin); using(SoundPlayer soundPlayer = new SoundPlayer(stream)) { soundPlayer.PlaySync(); } } } } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.playButton.Click += playButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 재생하기 버튼 클릭시 처리하기 - playButton_Click(sender, e) /// <summary> /// 재생하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void playButton_Click(object sender, EventArgs e) { SoundHelper.Beep(300, SoundHelper.C3, 100); SoundHelper.Beep(300, SoundHelper.D3, 100); SoundHelper.Beep(300, SoundHelper.E3, 100); SoundHelper.Beep(300, SoundHelper.F3, 100); SoundHelper.Beep(300, SoundHelper.A3, 100); SoundHelper.Beep(300, SoundHelper.B3, 100); } #endregion } } |