0

this is the code that i found online somewhere; it works quite well, but i dont fully understand how it convert a bunch of math into an audio wave:

    public static void Beeps(int Amplitude, int Frequency, int Duration)
    {
        double A = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
        double DeltaFT = 2 * Math.PI * Frequency / 44100.0;

        int Samples = 441 * Duration / 10;
        int Bytes = Samples * 4;
        int[] Hdr = 
        { 0X46464952, 36 + Bytes, 0X45564157, 
        0X20746D66, 16, 0X20001, 44100, 176400, 0X100004, 
        0X61746164, Bytes };
        using (MemoryStream MS = new MemoryStream(44 + Bytes))
        {
            using (BinaryWriter BW = new BinaryWriter(MS))
            {
                for (int I = 0; I < Hdr.Length; I++)
                {
                    BW.Write(Hdr[I]);
                }
                for (int T = 0; T < Samples; T++)
                {
                    short Sample = System.Convert.ToInt16(A * Math.Sin(DeltaFT * T));
                    BW.Write(Sample);
                    BW.Write(Sample);
                }
                BW.Flush();
                MS.Seek(0, SeekOrigin.Begin);
                using (SoundPlayer SP = new SoundPlayer(MS))
                {
                    SP.PlaySync();
                }
            }
        }
    }
REghZY
  • 69
  • 1
  • 7

2 Answers2

0

It looks like all it does is beep at certain pitches. The reason math converts into sound is because when the data is fed to your speaker, it's really bytes telling it how to vibrate during that instant.

If you're asking about how sound works, it's based on how vibrations move through the air. Vibrations exist as waves; they literally are shaking the air in certain patterns that your brain interprets as noise through your ears. If the sound has a higher pitch, the soundwaves are closer to each other, and if it's a lower pitch, they're further away. This is why a computer can "convert a bunch of math into an audio wave", because that's all sound really is: a constantly manipulated wave. That method takes a wavelength (Frequency) and creates a sine wave based on it, converts it to bytes, and feeds it to your speaker with a certain volume (Amplitude) and for a certain duration. Cool stuff right?

Also, you're looking at a "method", not a class. :)

Here's more about sound if you're interested: https://en.wikipedia.org/wiki/Sound#Sound_wave_properties_and_characteristics

el toro
  • 532
  • 4
  • 11
  • i know quite a bit about the science of sound and how speakers actually make sound, but what i sort of mean is how the computer converts that math into a sine wave, and then how it converts it to bytes (in fair detail, although that would be like teaching maths tbh, nothing to do with coding). Obviously at the end of the code, it just says PlaySync() which i guess makes outputting the audio much easier – REghZY Feb 28 '19 at 20:17
  • Well the sine wave is technically already math so I'm not sure what you mean there. I honestly don't know how the math itself works, since I haven't really studied anything about sound, I just recognize that it's making a sine wave that it plugs into the memorystream. So I guess I can't answer your question, sorry :/ `SP.PlaySync()` is just the `SoundPlayer` class's method of starting an audio waveform, like SqlConnection.Open() for SQL. – el toro Feb 28 '19 at 20:25
  • i guess 'plugging' the sine wave into the memory stream and then plugging that memory stream into a sound players make some logic, i kinda get that. but making the sine wave in code... wut??? tbh like i said it's most likely gonna require some complex math understandings – REghZY Feb 28 '19 at 20:47
  • Yeah, most likely. I understand the idea, but the math is above me personally. Maybe look for some documentation from the creator of the method. – el toro Feb 28 '19 at 20:49
0

This answer has a good overview of how wav files work:
Simply sample the waveform at fixed intervals, and write the amplitude at each interval into your file.
That's what the BW.Write calls are doing. T represents the Time.

In order to play the sound, that data goes after the Hdr section, which is simply the correct header for a standard .wav file. 0X46464952 is ascii for "RIFF" and 0X45564157 is "WAVE". The player needs to know what rate the wave was sampled at. In this case it's 44100, which is a common standard.

AShelly
  • 34,686
  • 15
  • 91
  • 152