Audio frequency generator: Difference between revisions

Added FreeBASIC
(→‎{{header|Julia}}: add Locomotive Basic version)
(Added FreeBASIC)
 
(13 intermediate revisions by 9 users not shown)
Line 1:
[[Category:Electronics]]
[[Category:Sciences]]
[[Category:Sound]]
[[Category:Temporal media]]
{{draft task}}
[[Category:Electronics]] [[Category:Sciences]]
[[Category:Sound]] [[Category:Temporal media]]
{{omit from|GUISS}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Scala}}
{{omit from|TPP}}
 
An audio [[wp:Signal_generator|frequency generator]] produces a continual audible monotone at a set frequency and level of volume.
There are controls to adjust the frequency and the volume up and down as desired.
Line 18 ⟶ 16:
 
* A demonstration of how to check for availability of sound hardware on the system (on systems where this is possible)
* A demonstration of how to produce a continual audible monotone (on sound hardware this would typicvallytypically be a sine wave)
* A method of adjusting the frequency and volume of the monotone. (one way would be to use left and right arrow keys to increase or decrease frequency, and up and down keys to increase or decrease the volume)
* A method to silence or reset the sound hardware on exit.
Line 29 ⟶ 27:
Languages that provide no facilities for utilizing sound hardware of any kind should be omitted.
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">byte
volu,dist,freq,key=764
 
proc INFO()
position(5,11)
printf("volume:%B freq:%B distortion:%B ",volu,freq,dist)
sound(0,freq,dist,volu)
key=255
return
 
proc INI()
freq=$46
volu=10
dist=10
INFO()
return
 
proc GENERATOR()
byte dmactls=559,cur=752,mar=82
card dlist=560
 
graphics(0) cur=1 mar=8
POKE(709,0) POKE(710,14)
POKEC(DLIST+8,0)
POKEC(DLIST+10,0)
POKEC(DLIST+19,0)
POKEC(DLIST+21,0)
POKEC(DLIST+26,0)
POKE (DLIST+28,0)
 
position(8,1)
printe("Action! sound generator")
printe("")
printe("")
printe("left/right - set volume")
printe("up/down - freq +/-")
printe("space - distorion")
printe("return - default (440 Hz)")
printe("esc - exit")
 
INI()
 
do
if key=28 then exit fi
if key=12 then INI() fi
if key=14 then freq==-1 INFO() fi
if key=15 then freq==+1 INFO() fi
if key=7 then volu==+1 if volu>14 then volu=15 fi INFO() fi
if key=6 then volu==-1 if volu=255 then volu=0 fi INFO() fi
if key=33 then dist==+2 if dist>15 then dist=0 fi INFO() fi
od
 
sndrst() mar=2 graphics(0)
return</syntaxhighlight>
=={{header|Axe}}==
{{untested|Axe}}
<langsyntaxhighlight lang="axe">ClrHome
Disp "FREQ:",i
10→F
Line 44 ⟶ 97:
Output(5,0,F▶Dec)
Freq(F,10000)
End</langsyntaxhighlight>
 
=={{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 50 ⟶ 156:
 
The duration of the monotone is set in advance (to a small number of seconds) and the application ends when it finishes playing. Consequently, a method to silence it is not required.
<langsyntaxhighlight lang="go">package main
 
import (
Line 118 ⟶ 224:
err := cmd.Run()
check(err)
}</langsyntaxhighlight>
 
 
=={{header|Julia}}==
Uses the PortAudio library.
<langsyntaxhighlight lang="julia">using PortAudio
 
if Sys.iswindows()
Line 236 ⟶ 342:
@async(inputtask(chan))
playtone(ostr)
</syntaxhighlight>
</lang>
 
=={{header|Locomotive Basic}}==
 
<langsyntaxhighlight lang="locobasic">10 mode 1:input "Enter initial frequency in Hz";f:cls
20 if sq(2)<128 then sound 2,62500/f,100
30 a$=inkey$
Line 246 ⟶ 352:
50 if a$="l" then f=f-10
60 if a$="q" then end
70 locate 1,1:print f"Hz "
80 print:print " Use h and l to adjust frequency;":print " q to quit."
90 goto 20</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Go}}
As in the Go version, we use the Sox "play" command to emulate the audio frequency generator. This version is a faithful translation of the Go version, even if the way things are done is pretty different.
<syntaxhighlight lang="nim">import osproc, strutils
 
type Waveform {.pure.} = enum
Sine = (1, "sine")
Square = (2, "square")
Sawtooth = (3, "sawtooth")
 
proc getIntValue(msg: string; minval, maxval: int): int =
while true:
stdout.write msg
stdout.flushFile()
try:
result = stdin.readLine.strip().parseInt()
if result notin minval..maxval:
echo "Invalid value"
else:
return
except ValueError:
echo "Error: invalid value."
except EOFError:
echo()
quit "Quitting.", QuitFailure
 
let freq = getIntValue("Enter frequency in Hz (40 to 10000): ", 40, 10_000)
let vol = getIntValue("Enter volume in dB (1 to 50): ", 1, 50)
let dur = getIntValue("Enter duration in seconds (2 to 10): ", 2, 10)
let kind = Waveform getIntValue("Enter kind (1 = sine, 2 = square, 3 = sawtooth): ", 1, 3)
 
let args = ["-n", "synth", $dur, $kind, $freq, "vol", $vol, "dB"]
echo execProcess("play", args = args, options = {poStdErrToStdOut, poUsePath})</syntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict 'vars';
use feature 'say';
use feature 'state';
Line 285 ⟶ 425:
return $freq;
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>-- demo/rosetta/Audio_frequency_generator.exw
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Audio_frequency_generator.exw</span>
include pGUI.e
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Ihandle dlg, frequency, duration
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">frequency</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">duration</span>
 
atom k32=0, xBeep
<span style="color: #004080;">atom</span> <span style="color: #000000;">k32</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xBeep</span>
 
function button_cb(Ihandle /*playbtn*/)
<span style="color: #008080;">function</span> <span style="color: #000000;">button_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*playbtn*/</span><span style="color: #0000FF;">)</span>
integer f = IupGetInt(frequency,"VALUE"),
<span style="color: #004080;">integer</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">frequency</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">),</span>
d = IupGetInt(duration,"VALUE")
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">duration</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">)</span>
if platform()=WINDOWS then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #008080;">then</span>
if k32=0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">k32</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
k32 = open_dll("kernel32.dll")
<span style="color: #000000;">k32</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">open_dll</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"kernel32.dll"</span><span style="color: #0000FF;">)</span>
xBeep = define_c_proc(k32, "Beep", {C_INT,C_INT})
<span style="color: #000000;">xBeep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_c_proc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k32</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Beep"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
c_proc(xBeep,{f,d})
<span style="color: #7060A8;">c_proc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xBeep</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">})</span>
else
<span style="color: #008080;">else</span>
system(sprintf("play -n synth %f sine %d", {d/1000, f}))
<span style="color: #000000;">system</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"play -n synth %f sine %d"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">d</span><span style="color: #0000FF;">/</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">}))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return IUP_DEFAULT
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function valuechanged_cb(Ihandle val)
<span style="color: #008080;">function</span> <span style="color: #000000;">valuechanged_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">val</span><span style="color: #0000FF;">)</span>
-- maintain the labels as the sliders are moved
<span style="color: #000080;font-style:italic;">-- maintain the labels as the sliders are moved</span>
Ihandle parent = IupGetParent(val),
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">parent</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupGetParent</span><span style="color: #0000FF;">(</span><span style="color: #000000;">val</span><span style="color: #0000FF;">),</span>
lbl = IupGetNextChild(parent, NULL)
<span style="color: #000000;">lbl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupGetNextChild</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parent</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
integer v = IupGetInt(val,"VALUE")
<span style="color: #004080;">integer</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">val</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">)</span>
IupSetInt(lbl,"TITLE",v)
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lbl</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
procedure main()
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
Ihandle flabel, dlabel, frame1, frame2, playbtn
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">flabel</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dlabel</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">frame1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">frame2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">playbtn</span>
IupOpen()
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
 
flabel = IupLabel("2000","ALIGNMENT=ARIGHT,NAME=val_label,SIZE=20x8")
<span style="color: #000000;">flabel</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"2000"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ALIGNMENT=ARIGHT,NAME=val_label,SIZE=20x8"</span><span style="color: #0000FF;">)</span>
frequency = IupValuator("HORIZONTAL","VALUECHANGED_CB", Icallback("valuechanged_cb"),
<span style="color: #000000;">frequency</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupValuator</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"HORIZONTAL"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUECHANGED_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"valuechanged_cb"</span><span style="color: #0000FF;">),</span>
"EXPAND=HORIZONTAL, CANFOCUS=NO, MIN=50, MAX=10000, VALUE=2000")
<span style="color: #008000;">"EXPAND=HORIZONTAL, CANFOCUS=NO, MIN=50, MAX=10000, VALUE=2000"</span><span style="color: #0000FF;">)</span>
frame1 = IupFrame(IupHbox({flabel,frequency}),"TITLE=\"Frequency (Hz): \"")
<span style="color: #000000;">frame1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupFrame</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">flabel</span><span style="color: #0000FF;">,</span><span style="color: #000000;">frequency</span><span style="color: #0000FF;">}),</span><span style="color: #008000;">"TITLE=\"Frequency (Hz): \""</span><span style="color: #0000FF;">)</span>
 
dlabel = IupLabel("500","ALIGNMENT=ARIGHT,NAME=val_label,SIZE=20x8")
<span style="color: #000000;">dlabel</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"500"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ALIGNMENT=ARIGHT,NAME=val_label,SIZE=20x8"</span><span style="color: #0000FF;">)</span>
duration = IupValuator("HORIZONTAL","VALUECHANGED_CB", Icallback("valuechanged_cb"),
<span style="color: #000000;">duration</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupValuator</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"HORIZONTAL"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUECHANGED_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"valuechanged_cb"</span><span style="color: #0000FF;">),</span>
"EXPAND=HORIZONTAL, CANFOCUS=NO, MIN=100, MAX=3000, VALUE=500")
<span style="color: #008000;">"EXPAND=HORIZONTAL, CANFOCUS=NO, MIN=100, MAX=3000, VALUE=500"</span><span style="color: #0000FF;">)</span>
frame2 = IupFrame(IupHbox({dlabel,duration}),"TITLE=\"Duration (ms): \"")
<span style="color: #000000;">frame2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupFrame</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">dlabel</span><span style="color: #0000FF;">,</span><span style="color: #000000;">duration</span><span style="color: #0000FF;">}),</span><span style="color: #008000;">"TITLE=\"Duration (ms): \""</span><span style="color: #0000FF;">)</span>
 
playbtn = IupHbox({IupFill(),
<span style="color: #000000;">playbtn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">IupFill</span><span style="color: #0000FF;">(),</span>
IupButton("Play",Icallback("button_cb"),"PADDING=30x0"),
<span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Play"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"button_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"PADDING=30x0"</span><span style="color: #0000FF;">),</span>
IupFill()},"MARGIN=0x20")
<span style="color: #7060A8;">IupFill</span><span style="color: #0000FF;">()},</span><span style="color: #008000;">"MARGIN=0x20"</span><span style="color: #0000FF;">)</span>
 
dlg = IupDialog(IupVbox({frame1,
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">frame1</span><span style="color: #0000FF;">,</span>
frame2,
playbtn}, "MARGIN<span style=10x5,"color: GAP#000000;">frame2</span><span style=5"))color: #0000FF;">,</span>
<span style="color: #000000;">playbtn</span><span style="color: #0000FF;">},</span> <span style="color: #008000;">"MARGIN=10x5, GAP=5"</span><span style="color: #0000FF;">))</span>
IupSetAttribute(dlg,"TITLE","Audio Frequency Generator")
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Audio Frequency Generator"</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg,"RASTERSIZE","500x230")
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"500x230"</span><span style="color: #0000FF;">)</span>
IupCloseOnEscape(dlg)
 
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
IupShow(dlg)
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
IupMainLoop()
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
IupClose()
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
end procedure
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
main()</lang>
<!--</syntaxhighlight>-->
 
=={{header|Pure Data}}==
Line 549 ⟶ 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}}==
<langsyntaxhighlight lang="ring">
# Project : Audio frequency generator
 
Line 714 ⟶ 887:
ok
return
</syntaxhighlight>
</lang>
Output:
 
[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}}==
SuperCollider is a sound programming language, so the task is predictably easy.
 
<syntaxhighlight lang="supercollider">
<lang SuperCollider>
// the server application detects audio hardware.
Server.default.boot;
Line 754 ⟶ 956:
// sound stops on exit
Server.default.quit;
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
{{libheader|Snack}}
This does not work on Windows due the use of the external <tt>stty</tt> program.
<langsyntaxhighlight lang="tcl">package require sound
 
set baseFrequency 261.63
Line 821 ⟶ 1,023:
# Stop the sound playing
$sound stop
exit</langsyntaxhighlight>
 
=={{header|Wren}}==
{{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_generator.wren */
 
class C {
foreign static getInput(maxSize)
 
foreign static play(args)
}
 
var freq = 0
while (!freq || !freq.isInteger || freq < 40 || freq > 10000) {
System.write("Enter frequency in HZ (40 to 10000) : ")
freq = Num.fromString(C.getInput(5))
}
var freqS = freq.toString
 
var vol = 0
while (!vol || vol < 1 || vol > 50) {
System.write("Enter volume in dB (1 to 50) : ")
vol = Num.fromString(C.getInput(2))
}
var volS = vol.toString
 
var dur = 0
while (!dur || dur < 2 || dur > 10) {
System.write("Enter duration in seconds (2 to 10) : ")
dur = Num.fromString(C.getInput(2))
}
var durS = dur.toString
 
var kind = 0
while (!kind || !kind.isInteger || kind < 1 || kind > 3) {
System.write("Enter kind (1 = Sine, 2 = Square, 3 = Sawtooth) : ")
kind = Num.fromString(C.getInput(1))
}
var kindS = "sine"
if (kind == 2) {
kindS = "square"
} else if (kind == 3) {
kindS = "sawtooth"
}
 
var args = ["-n", "-V1", "synth", durS, kindS, freqS, "vol", volS, "dB"].join(" ")
C.play(args)</syntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include "wren.h"
 
void C_getInput(WrenVM* vm) {
int maxSize = (int)wrenGetSlotDouble(vm, 1) + 2;
char input[maxSize];
fgets(input, maxSize, stdin);
__fpurge(stdin);
input[strcspn(input, "\n")] = 0;
wrenSetSlotString(vm, 0, (const char*)input);
}
 
void C_play(WrenVM* vm) {
const char *args = wrenGetSlotString(vm, 1);
char command[strlen(args) + 5];
strcpy(command, "play ");
strcat(command, args);
system(command);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "getInput(_)") == 0) return C_getInput;
if (isStatic && strcmp(signature, "play(_)") == 0) return C_play;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Audio_frequency_generator.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
Line 833 ⟶ 1,177:
There is no volume control on the Spectrum.
 
<langsyntaxhighlight lang="zxbasic">10 REM The crappest signal generator in the world
20 REM We do not check for boundary errors in this simple demo
30 LET n=1
Line 841 ⟶ 1,185:
70 PRINT AT 0,0;n," "
80 BEEP 0.1,n: REM beep for 0.1 second at n semitones relative to middle C
90 GO TO 40</langsyntaxhighlight>
{{omit from|GUISS}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Scala}}
{{omit from|TPP}}
2,122

edits