54 lines
1.6 KiB
Plaintext
54 lines
1.6 KiB
Plaintext
#define PI 4 * Atn(1)
|
|
|
|
' Constants for the audio format
|
|
Dim Shared As Integer SAMPLE_RATE = 44100
|
|
Dim Shared As Integer BITS_PER_SAMPLE = 16
|
|
Dim Shared As Integer NUM_CHANNELS = 1
|
|
|
|
' Generates a sine wave
|
|
Sub generateSineWave(buffer() As Short, frequency As Double)
|
|
Dim As Double increment = 2.0 * PI * frequency / SAMPLE_RATE
|
|
Dim As Double x = 0.0
|
|
For i As Integer = 0 To Ubound(buffer)
|
|
buffer(i) = (Sin(x) * 32767.0)
|
|
x += increment
|
|
Next i
|
|
End Sub
|
|
|
|
' Write the header of the .wav file
|
|
Sub writeWaveHeader(file As Integer, numSamples As Integer)
|
|
' Write the RIFF header
|
|
Print #file, "RIFF";
|
|
Put #file, , numSamples * 2 + 36 ' File size
|
|
Print #file, "WAVE";
|
|
|
|
Dim As Integer SB = 16, FA = 1
|
|
' Write the fmt sub-block
|
|
Print #file, "fmt ";
|
|
Put #file, , SB ' Size of the fmt sub-block
|
|
Put #file, , FA ' Audio format (1 = PCM)
|
|
Put #file, , NUM_CHANNELS ' Number of channels
|
|
Put #file, , SAMPLE_RATE ' Sample rate
|
|
Put #file, , SAMPLE_RATE * NUM_CHANNELS * BITS_PER_SAMPLE / 8 ' Byte rate
|
|
Put #file, , NUM_CHANNELS * BITS_PER_SAMPLE / 8 ' Alineación de bloques
|
|
Put #file, , BITS_PER_SAMPLE ' Bits per sample
|
|
|
|
' Write the data sub-block
|
|
Print #file, "data";
|
|
Put #file, , numSamples * 2 ' Data size
|
|
End Sub
|
|
|
|
Dim As Integer file = Freefile
|
|
Open "output.wav" For Binary As #file
|
|
|
|
' Generates a 440 Hz sine wave for 5 seconds
|
|
Dim As Integer numSamples = SAMPLE_RATE * 5
|
|
Dim As Short buffer(numSamples - 1)
|
|
generateSineWave(buffer(), 440.0)
|
|
|
|
' Write the .wav file
|
|
writeWaveHeader(file, numSamples)
|
|
Put #file, , buffer()
|
|
|
|
Close #file
|