3 class ToneSound : private Sound
9 void GenerateTone(float freq, int duration, double volume)
11 uint length = this.length = frequency * duration;
12 short * data = (short *)(this.data = (byte *)new short[length]);
15 for(i = 0; i < length; i++)
17 Angle x = 2 * Pi * i * freq / frequency;
18 ((short *)data)[i] = (short)(sin(x) * volume * 32767);
23 class BeepForm : Window
26 background = activeBorder;
27 borderStyle = sizable;
38 this, caption = "Play", position = { 200, 168 };
40 bool NotifyClicked(Button button, int x, int y, Modifiers mods)
55 samples = AUDIO_BUFFER_SIZE;
56 callback = AudioCallback;
58 windowHandle = systemHandle;
63 sound.GenerateTone(440, 1, 0.25); // 1 second of 440Hz A at quarter volume
65 if(OpenAudio(wantedSpec, spec) < 0)
67 MessageBox { contents = "OpenAudio epic fail" }.Modal();
78 void AudioCallback(byte *stream, int len)
80 static byte buffer[AUDIO_BUFFER_SIZE];
81 int s = Min(sound.length * (sound.bits == 16 ? 2 : 1) - pos, len);
82 memcpy(buffer, sound.data + pos, s);
87 memset(buffer + s, 128, len - s);
89 memset(buffer + s, 0, len - s);
91 memcpy(stream, buffer, len);