Handmade Hero»Forums»Code
1 posts
None
[Day 20] Tiny bit of fun with the code
Edited by WilWa on
I am way late to the party, only on day 20, but I thought I'd share a tiny bit of fun I had with the code after that long video. (It's nothing special...just playing around a little, and I thought it might be interesting to someone else. In addition to 350 with 440, also try 440 with 480 and 480 with 620.)

 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
internal void
GameOutputSound(game_sound_output_buffer *SoundBuffer, int ToneHz)
{
  local_persist real32 tSine1;
  local_persist real32 tSine2;
  int16 ToneVolume = 3000;
  int WavePeriod1 = SoundBuffer->SamplesPerSecond / 350;
  int WavePeriod2 = SoundBuffer->SamplesPerSecond / 440;

  int16 *SampleOut = SoundBuffer->Samples;
  for (int SampleIndex = 0; SampleIndex < SoundBuffer->SampleCount; ++SampleIndex)
  {
    // TODO(casey): Draw this out for people
    real32 SineValue1 = sinf(tSine1);
    real32 SineValue2 = sinf(tSine2);
    int16 SampleValue = (int16)((SineValue1+SineValue2) * ToneVolume);
    *SampleOut++ = SampleValue;
    *SampleOut++ = SampleValue;

    tSine1 += 2.0f * Pi32 * 1.0f / (real32)WavePeriod1;
    if (tSine1 > 2.0f * Pi32)
    {
      tSine1 -= 2.0f * Pi32;
    }
    tSine2 += 2.0f * Pi32 * 1.0f / (real32)WavePeriod2;
    if (tSine2 > 2.0f * Pi32)
    {
      tSine2 -= 2.0f * Pi32;
    }        
  }
}
75 posts
None
[Day 20] Tiny bit of fun with the code
Cool. Sounds like a dial tone.