Audio frequency generator: Difference between revisions

Added FreeBASIC
m (→‎{{header|Wren}}: Changed to Wren S/H)
(Added FreeBASIC)
 
(3 intermediate revisions by 3 users not shown)
Line 98:
Freq(F,10000)
End</syntaxhighlight>
 
=={{header|FreeBASIC}}==
[https://www.un4seen.com/ Library: BASS]
<syntaxhighlight lang="vbnet">#include "bass.bi"
#include "fbgfx.bi"
 
Dim Shared As Integer freq = 440, volume = 50
Dim Shared As HSTREAM stream
 
Sub CheckKeys()
If Multikey(SC_LEFT) Then freq -= 10
If Multikey(SC_RIGHT) Then freq += 10
If Multikey(SC_UP) Then volume += 5
If Multikey(SC_DOWN) Then volume -= 5
' Limit frequency and volume to reasonable values
If freq < 20 Then freq = 20
If freq > 20000 Then freq = 20000
If volume < 0 Then volume = 0
If volume > 100 Then volume = 100
' Update the stream with the new frequency and volume
BASS_ChannelSetAttribute(stream, BASS_ATTRIB_FREQ, freq)
BASS_ChannelSetAttribute(stream, BASS_ATTRIB_VOL, volume / 100.0)
End Sub
 
' Initialize BASS using the default device at 44.1 KHz.
If (BASS_Init(-1, 44100, 0, 0, 0) = False) Then
Print "Could not initialize audio! BASS returned error " & BASS_ErrorGetCode()
Sleep
End
End If
 
' Create a stream
stream = BASS_StreamCreate(44100, 1, 0, @BASS_STREAMPROC_PUSH, NULL)
If stream = 0 Then
Print "Error creating stream: "; BASS_ErrorGetCode()
BASS_Free
Sleep
End 1
End If
 
' Start the stream
BASS_ChannelPlay(stream, False)
 
Do
CheckKeys
Sleep 10
Loop Until Inkey$ <> ""
 
' Clean up
BASS_StreamFree(stream)
BASS_Free</syntaxhighlight>
 
=={{header|Go}}==
Line 637 ⟶ 690:
* Volume level and wave shape are graphically displayed.
* More shapes, even free wave shape modelling could easily be added.
 
=={{header|Raku}}==
{{trans|Go}}
<syntaxhighlight lang="raku" line># 20240130 Raku programming solution
 
my ($kindS, $kind, $freq, $vol, $dur) = 'sine', |(0 xx *);
 
while $freq < 40 || $freq > 10000 {
print "Enter frequency in Hz (40 to 10000) : ";
$freq = prompt().Int;
}
 
while $vol < 1 || $vol > 50 {
print "Enter volume in dB (1 to 50) : ";
$vol = prompt().Int;
}
 
while $dur < 2 || $dur > 10 {
print "Enter duration in seconds (2 to 10) : ";
$dur = prompt().Int;
}
 
while $kind < 1 || $kind > 3 {
print 'Enter kind (1 = Sine, 2 = Square, 3 = Sawtooth) : ';
given $kind = prompt().Int {
when $kind == 2 { $kindS = 'square' }
when $kind == 3 { $kindS = 'sawtooth' }
}
}
 
my @args = "-n", "synth", $dur, $kindS, $freq, "vol", $vol, "dB";
run 'play', @args, :out('/dev/null'), :err('/dev/null');</syntaxhighlight>
 
=={{header|Ring}}==
Line 806 ⟶ 891:
 
[https://www.dropbox.com/s/jf5uq0bbts40wto/CalmoSoftFrequency.avi?dl=0 Audio frequency generator]
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">var (kindS = 'sine', kind = 0, freq = 0, vol = 0, dur = 0)
 
while ((freq < 40) || (freq > 10000)) {
freq = (Sys.read("Enter frequency in Hz (40 to 10000) : ", Num) || next)
}
 
while ((vol < 1) || (vol > 50)) {
vol = (Sys.read("Enter volume in dB (1 to 50) : ", Num) || next)
}
 
while ((dur < 2) || (dur > 10)) {
dur = (Sys.read("Enter duration in seconds (2 to 10) : ", Num) || next)
}
 
while ((kind < 1) || (kind > 3)) {
kind = (Sys.read("Enter kind (1 = Sine, 2 = Square, 3 = Sawtooth) : ", Num) || next)
 
given(kind) {
when (1) { kindS = 'sine' }
when (2) { kindS = 'square' }
when (3) { kindS = 'sawtooth' }
}
}
 
var args = ["-n", "synth", dur, kindS, freq, "vol", vol, "dB"]
Sys.run('play', args...)</syntaxhighlight>
 
=={{header|SuperCollider}}==
Line 914 ⟶ 1,028:
{{trans|Go}}
The ability to call external processes such as ''SoX'' is expected to be added to Wren-cli in the next release. In the meantime, we embed the following Wren script in a C host to complete this task.
<syntaxhighlight lang="wren">/* audio_frequency_generatorAudio_frequency_generator.wren */
 
class C {
Line 1,035 ⟶ 1,149:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "audio_frequency_generatorAudio_frequency_generator.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
2,122

edits