Audio frequency generator: Difference between revisions

Added FreeBASIC
(→‎Tcl: Added implementation)
(Added FreeBASIC)
 
(33 intermediate revisions by 17 users not shown)
Line 1:
[[Category:Electronics]]
[[Category:Sciences]]
[[Category:Sound]]
[[Category:Temporal media]]
{{draft task}}
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.
Some also have a selector to switch the waveform type between sine wave, square wave and triangular sawtooth.
 
The task is to emulate an audio frequency generator.
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. Some also have a selector to switch the waveform type between sine wave, square wave and triangular sawtooth.
It is permissible to use an inbuilt computer speaker
 
The task is to emulate an audio frequency generator. It is permissible to use an inbuilt computer speaker if the system does not have the facility to utilize dedicated sound hardware.
 
The solution should include:
 
* 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 18 ⟶ 26:
 
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}}
<syntaxhighlight lang="axe">ClrHome
Disp "FREQ:",i
10→F
Repeat getKey(15)
If getKey(3)
F++
F=0?-1→F
ElseIf getKey(2)
F--
F=-1?0→F
End
Output(5,0,F▶Dec)
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}}==
As Go does not have any audio support in its standard library, this invokes the SoX utility's 'play' command with the appropriate parameters to emulate an audio frequency generator. It appears that SoX automatically uses the internal speaker if there is no sound hardware available.
 
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.
<syntaxhighlight lang="go">package main
 
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strconv"
)
 
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
 
func main() {
scanner := bufio.NewScanner(os.Stdin)
freq := 0
for freq < 40 || freq > 10000 {
fmt.Print("Enter frequency in Hz (40 to 10000) : ")
scanner.Scan()
input := scanner.Text()
check(scanner.Err())
freq, _ = strconv.Atoi(input)
}
freqS := strconv.Itoa(freq)
 
vol := 0
for vol < 1 || vol > 50 {
fmt.Print("Enter volume in dB (1 to 50) : ")
scanner.Scan()
input := scanner.Text()
check(scanner.Err())
vol, _ = strconv.Atoi(input)
}
volS := strconv.Itoa(vol)
 
dur := 0.0
for dur < 2 || dur > 10 {
fmt.Print("Enter duration in seconds (2 to 10) : ")
scanner.Scan()
input := scanner.Text()
check(scanner.Err())
dur, _ = strconv.ParseFloat(input, 64)
}
durS := strconv.FormatFloat(dur, 'f', -1, 64)
 
kind := 0
for kind < 1 || kind > 3 {
fmt.Print("Enter kind (1 = Sine, 2 = Square, 3 = Sawtooth) : ")
scanner.Scan()
input := scanner.Text()
check(scanner.Err())
kind, _ = strconv.Atoi(input)
}
kindS := "sine"
if kind == 2 {
kindS = "square"
} else if kind == 3 {
kindS = "sawtooth"
}
 
args := []string{"-n", "synth", durS, kindS, freqS, "vol", volS, "dB"}
cmd := exec.Command("play", args...)
err := cmd.Run()
check(err)
}</syntaxhighlight>
 
 
=={{header|Julia}}==
Uses the PortAudio library.
<syntaxhighlight lang="julia">using PortAudio
 
if Sys.iswindows()
getch() = @threadcall((:_getch, "msvcr100.dll"), Cint, ())
else
getch() = @threadcall((:getch, libcurses), Cint, ())
end
 
function audiodevices()
println(rpad("Index", 6), rpad("Device Name", 44), rpad("API", 16), rpad("In", 4),
rpad("Out", 4), rpad("Sample Rate", 8))
for (i, dev) in enumerate(PortAudio.devices())
println(rpad(i - 1, 6), rpad(dev.name, 44), rpad(dev.hostapi, 16),
rpad(dev.maxinchans, 4), rpad(dev.maxoutchans, 4),
rpad(dev.defaultsamplerate, 8))
end
end
 
println("Listing available hardware:")
audiodevices()
 
function paudio()
devs = PortAudio.devices()
devnum = findfirst(x -> x.maxoutchans > 0, devs)
(devnum == nothing) && error("No output device for audio found")
println("Enter a device # from the above, or Enter for default: ")
n = tryparse(Int, strip(readline()))
devnum = n == nothing ? devnum : n + 1
return PortAudioStream(devs[devnum].name, 0, 2)
end
 
play(ostream, sample::Array{Float64,1}) = write(ostream, sample)
play(ostr, sample::Array{Int64,1}) = play(ostr, Float64.(sample))
 
struct Note{S<:Real, T<:Real}
pitch::S
duration::T
volume::T
sustained::Bool
end
 
sinewave(t) = 0.6sin(t) + 0.2sin(2t) + .05*sin(8t)
squarewave(t) = iseven(Int(trunc(t / π))) ? 1.0 : -1.0
sawtoothwave(t) = rem(t, 2π)/π - 1
 
function play(ostream, A::Note, samplingfreq::Real=44100, shape::Function=sinewave, pause=true)
timesamples = 0:(1 / samplingfreq):(A.duration * (A.sustained ? 0.98 : 0.9))
v = Float64[shape(2π * A.pitch * t) for t in timesamples]
if !A.sustained
decay_length = div(length(timesamples), 5)
v[end-decay_length:end-1] = v[end-decay_length:end-1] .* LinRange(1, 0, decay_length)
end
v .*= A.volume
play(ostream, v)
if pause
sleep(A.duration)
end
end
 
function inputtask(channel)
println("""
\nAllow several seconds for settings changes to take effect.
Arrow keys:
Volume up: up Volume down: down Frequency up: right Frequency down: left
Sine wave(default): s Square wave: a Sawtooth wave: w
To exit: x
""")
while true
inputch = Char(getch())
if inputch == 'à'
inputch = Char(getch())
end
put!(channel, inputch)
sleep(0.2)
end
end
 
function playtone(ostream)
volume = 0.5
pitch = 440.0
waveform = sinewave
while true
if isready(chan)
ch = take!(chan)
if ch == 'H'
volume = min(volume * 2.0, 1.0)
elseif ch == 'P'
volume = max(volume * 0.5, 0.0)
elseif ch == 'M'
pitch = min(pitch * 9/8, 20000)
elseif ch == 'K'
pitch = max(pitch * 7/8, 32)
elseif ch == 's'
waveform = sinewave
elseif ch == 'a'
waveform = squarewave
elseif ch == 'w'
waveform = sawtoothwave
elseif ch == 'x'
break
end
end
play(ostream, Note(pitch, 4.5, volume, true), 44100.0, waveform, false)
end
exit()
end
 
const ostr = paudio()
const chan = Channel{Char}(1)
const params = Any[]
 
@async(inputtask(chan))
playtone(ostr)
</syntaxhighlight>
 
=={{header|Locomotive Basic}}==
 
<syntaxhighlight 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$
40 if a$="h" then f=f+10
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</syntaxhighlight>
 
=={{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}}==
<syntaxhighlight lang="perl">use strict 'vars';
use feature 'say';
use feature 'state';
use Audio::NoiseGen qw(play sine square triangle);
use Term::ReadKey qw(ReadMode ReadLine);
 
Audio::NoiseGen::init() || die 'No access to sound hardware?';
 
print "Play [S]ine, s[Q]uare or [T]riangle wave? "; my $ans_freq = uc(<>);
print "Pick a volume [0-9]"; my $ans_volume = <>;
say 'Volume: '. (my $volume = 0.1 + 1 * $ans_volume/10);
 
ReadMode(3);
 
my $waveform = $ans_freq eq 'Q' ? 'square' : $ans_freq eq 'T' ? 'triangle' : 'sine';
play ( gen => amp ( amount => $volume, gen => &$waveform( freq => setfreq(440) ) ) );
 
sub setfreq {
state $freq;
say $freq = shift;
return sub {
ReadMode(3);
state $cnt;
unless ($cnt++ % 1000) {
my $key = ReadLine(-1);
my $previous = $freq;
if ($key eq "\e[A") { $freq += 10 }
elsif ($key eq "\e[B") { $freq -= 10 }
elsif ($key eq "\e[C") { $freq += 100 }
elsif ($key eq "\e[D") { $freq -= 100 }
say $freq if $freq != $previous;
}
return $freq;
}
}</syntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Audio_frequency_generator.exw</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<span style="color: #008080;">else</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<span style="color: #000080;font-style:italic;">-- maintain the labels as the sliders are moved</span>
<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>
<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>
<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>
<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>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<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>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<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>
<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>
<span style="color: #008000;">"EXPAND=HORIZONTAL, CANFOCUS=NO, MIN=50, MAX=10000, VALUE=2000"</span><span style="color: #0000FF;">)</span>
<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>
<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>
<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>
<span style="color: #008000;">"EXPAND=HORIZONTAL, CANFOCUS=NO, MIN=100, MAX=3000, VALUE=500"</span><span style="color: #0000FF;">)</span>
<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>
<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>
<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>
<span style="color: #7060A8;">IupFill</span><span style="color: #0000FF;">()},</span><span style="color: #008000;">"MARGIN=0x20"</span><span style="color: #0000FF;">)</span>
<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>
<span style="color: #000000;">frame2</span><span style="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>
<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>
<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>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Pure Data}}==
'''audio-frequency-generator.pd'''
<pre>
#N canvas 245 70 635 516 10;
#X obj 501 214 cnv 15 84 66 empty empty empty 80 12 0 10 -262130 -13381 0;
#X obj 319 98 cnv 15 133 15 empty empty frequency 74 8 0 10 -204786 -13381 0;
#X obj 319 55 line;
#X floatatom 319 98 8 0 0 1 - - -;
#X obj 109 436 dac~;
#X obj 322 261 hsl 128 15 0 1 0 0 empty empty volume 90 8 0 10 -203904 -1 -4160 0 0;
#X obj 85 142 osc~;
#X floatatom 554 34 5 0 0 0 MIDI_note_number - -;
#X obj 554 52 mtof;
#X obj 32 142 phasor~;
#X obj 119 371 *~;
#X obj 227 345 line~;
#X floatatom 319 285 5 0 0 0 - - -;
#X floatatom 154 444 5 0 0 1 dB - -;
#X obj 154 423 env~ 256;
#X obj 154 462 - 100;
#X obj 320 357 vu 15 120 empty empty -1 -8 0 10 -66577 -1 1 0;
#X msg 227 323 \$1 20;
#X obj 32 292 *~;
#X obj 84 293 *~;
#X obj 120 293 *~;
#X obj 319 142 tgl 15 0 empty empty sawtooth 20 7 0 10 -261234 -1 -258113 0 1;
#X obj 319 165 tgl 15 0 empty empty sine 20 7 0 10 -261234 -1 -258113 0 1;
#X obj 319 188 tgl 15 0 empty empty square 20 7 0 10 -261234 -1 -258113 0 1;
#N canvas 0 0 450 300 (subpatch) 0;
#X array graph 100 float 0;
#X coords 0 1 99 -1 200 140 1 0 0;
#X restore 386 339 graph;
#X obj 63 239 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -228856 -1;
#X obj 173 142 triang~;
#X obj 172 293 *~;
#X obj 319 211 tgl 15 0 empty empty triangle 20 7 0 10 -261234 -1 -258113 0 1;
#X obj 120 142 square~;
#X obj 226 142 pulse~;
#X obj 227 293 *~;
#X obj 319 234 tgl 15 0 empty empty pulse 20 7 0 10 -261234 -1 -258113 0 1;
#X obj 32 164 -~ 0.5;
#X obj 45 462 tabwrite~ graph;
#X obj 529 193 loadbang;
#X msg 529 215 \; pd dsp 1;
#X msg 319 34 18 \, 24000 30000;
#X msg 529 250 \; pd dsp 0;
#X text 509 223 on;
#X text 503 258 off;
#X msg 32 34 50;
#X msg 63 34 100;
#X msg 94 34 200;
#X msg 126 34 500;
#X msg 157 34 1000;
#X msg 195 34 2000;
#X msg 232 34 4000;
#X msg 268 34 8000;
#X connect 2 0 3 0;
#X connect 3 0 6 0;
#X connect 3 0 9 0;
#X connect 3 0 25 0;
#X connect 3 0 26 0;
#X connect 3 0 29 0;
#X connect 3 0 30 0;
#X connect 5 0 12 0;
#X connect 5 0 25 0;
#X connect 6 0 19 0;
#X connect 7 0 8 0;
#X connect 8 0 3 0;
#X connect 9 0 33 0;
#X connect 10 0 4 0;
#X connect 10 0 4 1;
#X connect 10 0 14 0;
#X connect 10 0 34 0;
#X connect 11 0 10 1;
#X connect 12 0 17 0;
#X connect 13 0 15 0;
#X connect 14 0 13 0;
#X connect 15 0 16 0;
#X connect 17 0 11 0;
#X connect 18 0 10 0;
#X connect 19 0 10 0;
#X connect 20 0 10 0;
#X connect 21 0 18 1;
#X connect 21 0 25 0;
#X connect 22 0 19 1;
#X connect 22 0 25 0;
#X connect 23 0 20 1;
#X connect 23 0 25 0;
#X connect 25 0 34 0;
#X connect 26 0 27 0;
#X connect 27 0 10 0;
#X connect 28 0 27 1;
#X connect 28 0 25 0;
#X connect 29 0 20 0;
#X connect 30 0 31 0;
#X connect 31 0 10 0;
#X connect 32 0 25 0;
#X connect 32 0 31 1;
#X connect 33 0 18 0;
#X connect 35 0 36 0;
#X connect 37 0 2 0;
#X connect 41 0 3 0;
#X connect 42 0 3 0;
#X connect 43 0 3 0;
#X connect 44 0 3 0;
#X connect 45 0 3 0;
#X connect 46 0 3 0;
#X connect 47 0 3 0;
#X connect 48 0 3 0;
</pre>
'''square~.pd'''
<pre>
#N canvas 787 211 450 300 10;
#X obj 46 17 inlet;
#X obj 112 86 * -1;
#X obj 46 109 phasor~;
#X obj 46 186 -~ 1;
#X obj 112 109 phasor~;
#X obj 151 17 loadbang;
#X msg 108 59 0;
#X obj 47 223 outlet~;
#X msg 151 59 0.5;
#X obj 46 165 +~;
#X obj 108 17 inlet;
#X connect 0 0 1 0;
#X connect 0 0 2 0;
#X connect 1 0 4 0;
#X connect 2 0 9 0;
#X connect 3 0 7 0;
#X connect 4 0 9 1;
#X connect 5 0 6 0;
#X connect 5 0 8 0;
#X connect 6 0 2 1;
#X connect 8 0 4 1;
#X connect 9 0 3 0;
#X connect 10 0 6 0;
#X connect 10 0 8 0;
</pre>
'''triang~.pd'''
<pre>
#N canvas 770 214 450 300 10;
#X obj 46 17 inlet;
#X obj 112 51 * -1;
#X obj 46 74 phasor~;
#X obj 46 95 *~ 2;
#X obj 46 116 -~ 1;
#X obj 46 137 clip~ 0 1;
#X obj 112 74 phasor~;
#X obj 112 95 *~ 2;
#X obj 112 116 -~ 1;
#X obj 112 137 clip~ 0 1;
#X obj 194 18 loadbang;
#X msg 194 39 0;
#X obj 47 187 +~;
#X obj 47 208 -~ 0.5;
#X obj 47 229 *~ 2;
#X obj 47 250 outlet~;
#X obj 156 18 inlet;
#X connect 0 0 1 0;
#X connect 0 0 2 0;
#X connect 1 0 6 0;
#X connect 2 0 3 0;
#X connect 3 0 4 0;
#X connect 4 0 5 0;
#X connect 5 0 12 0;
#X connect 6 0 7 0;
#X connect 7 0 8 0;
#X connect 8 0 9 0;
#X connect 9 0 12 1;
#X connect 10 0 11 0;
#X connect 11 0 2 1;
#X connect 11 0 6 1;
#X connect 12 0 13 0;
#X connect 13 0 14 0;
#X connect 14 0 15 0;
#X connect 16 0 11 0;
</pre>
'''pulse~.pd'''
<pre>
#N canvas 784 384 450 300 10;
#X obj 51 56 phasor~;
#X obj 51 77 -~ 0.99;
#X obj 51 98 clip~ 0 1;
#X obj 51 119 *~ 100;
#X obj 51 140 outlet~;
#X obj 51 34 inlet;
#X obj 90 34 inlet;
#X connect 0 0 1 0;
#X connect 1 0 2 0;
#X connect 2 0 3 0;
#X connect 3 0 4 0;
#X connect 5 0 0 0;
#X connect 6 0 0 1;
</pre>
* Provides on/off switch, frequency and volume control and five different wave shapes to select from.
* Frequency may be typed in or changed by dragging. Predefined pitches and a frequency ramp (18 - 24000 Hz) are accessible by clicking the respective labels.
* 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}}==
<syntaxhighlight lang="ring">
# Project : Audio frequency generator
 
Load "guilib.ring"
loadlib("C:\Ring\extensions\ringbeep\ringbeep.dll")
 
freq = 1000
ImageFile = "stock.jpg"
 
UserIcons = CurrentDir() +"\"
 
WinLeft = 80
WinTop = 80
WinWidth = 1200
WinHeight = 750
WinRight = WinLeft + WinWidth
WinBottom = WinTop + WinHeight
 
BoxLeft = 80
BoxTop = 40
BoxWidth = WinWidth -160
BoxHeight = WinHeight -100
imageW = 400 ; imageH = 400 ; GrowBy = 8
volume = 100
 
MyApp = New qapp
{
 
win1 = new qMainWindow()
{
setwindowtitle("Video and Music Player")
setgeometry( WinLeft, WinTop, WinWidth, WinHeight)
 
if Fexists(ImageFile)
 
imageStock = new qlabel(win1)
{
image = new qpixmap(ImageFile)
AspectRatio = image.width() / image.height()
 
imageW = 1000
imageH = 600
 
setpixmap(image.scaled(imageW , imageH ,0,0))
PosLeft = (BoxWidth - imageW ) / 2 + 80
PosTop = (BoxHeight - imageH ) / 2 +40
setGeometry(PosLeft,PosTop,imageW,imageH)
 
}
 
else
msg = "ImageFile: -- "+ ImageFile +" -- required. Use an Image JPG of your choice"
SendMsg(msg)
ok
 
videowidget = new qVideoWidget(win1)
{
setgeometry(BoxLeft, BoxTop, BoxWidth, BoxHeight)
setstylesheet("background-color: green")
}
 
player = new qMediaPlayer()
{
setVideoOutput(videowidget)
}
 
TimerDuration = new qTimer(win1)
{
setinterval(1000)
settimeoutevent("pTimeDuration()") ### ==>> func
start()
}
 
oFont = new qFont("",10,0,0)
setFont( oFont)
 
btnBack = new qpushbutton(win1) {
setGeometry(280,20,80,20)
settext("Low")
seticon(new qicon(new qpixmap(UserIcons +"Backward.png")))
setclickevent( "pBackward()")
}
 
btnDur = new qpushbutton(win1) {
setGeometry(360,20,140,20)
}
 
btnFwd = new qpushbutton(win1) {
setGeometry(500,20,80,20)
settext("High")
seticon(new qicon(new qpixmap(UserIcons +"Forward.png")))
setclickevent( "pForward()")
}
 
btnVolume = new qpushbutton(win1) {
setGeometry(760,20,100,20)
settext("Volume: 100")
seticon(new qicon(new qpixmap(UserIcons +"Volume.png")))
}
 
VolumeDec = new qpushbutton(win1)
{
setgeometry(700,20,60,20)
settext("Low")
seticon(new qicon(new qpixmap(UserIcons +"VolumeLow.png")))
setclickevent( "PVolumeDec()")
}
 
VolumeInc = new qpushbutton(win1)
{
setgeometry(860,20,60,20)
settext("High")
seticon(new qicon(new qpixmap(UserIcons +"VolumeHigh.png")))
setclickevent( "pVolumeInc()")
}
 
show()
 
}
 
exec()
}
 
Func pTimeDuration()
Duration()
return
 
Func Duration()
 
DurPos = "Frequency: " + string(freq) + " Hz"
btnDur.setText(DurPos)
 
return
 
Func pForward
freq = freq + 100
for n = 1 to 3
beep(freq,300)
next
return
 
Func pBackward
freq = freq - 100
for n = 1 to 3
beep(freq,300)
next
return
 
Func pVolumeDec()
if volume > 0
volume = volume - 10
btnVolume.settext("Volume: " + volume)
player.setVolume(volume)
ok
return
 
Func pVolumeInc()
if volume < 100
volume = volume + 10
btnVolume.settext("Volume: " + volume)
player.setVolume(volume)
ok
return
</syntaxhighlight>
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">
// the server application detects audio hardware.
Server.default.boot;
 
// play a sine monotone at 440 Hz and amplitude 0.2
{ SinOsc.ar(440) * 0.2 }.play;
 
// use the cursor position to adjust frequency and amplitude (ranges are exponential)
{ SinOsc.ar(MouseX.kr(40, 20000, 1)) * MouseY.kr(0.001, 0.5, 1) }.play;
 
// use the cursor position to switch between sine wave, square wave and triangular sawtooth
// the rounding and lag smoothes the transition between them
{ SelectX.ar(MouseX.kr(0, 2).round.lag, [ SinOsc.ar, Pulse.ar, LFTri.ar ]) * 0.1 }.play;
 
// the same expressed as an array of functions of a common phase
(
{
var phase = LFSaw.ar;
var functions = [
{ |x| sin(x * pi) },
{ |x| x > 0 },
{ |x| abs(x) },
];
var which = MouseX.kr(0, 2);
functions.sum { |f, i|
abs(which - i) < 0.5 * f.(phase)
} * 0.1
}.play
)
 
// sound stops on exit
Server.default.quit;
</syntaxhighlight>
 
=={{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 84 ⟶ 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}}==
 
The ZX Spectrum is not very good at making sound.
The ZX Spectrum is not very good at making sound. Most applications in BASIC would just produce annoying beeps, and the following is no exception. To emulate the signal generator, we just produce repetative beeps using the inbuilt speaker. The left and right keys (5 and 8) change the tone. There is no volume control on the Spectrum.
Most applications in BASIC would just produce annoying beeps,
and the following is no exception.
To emulate the signal generator, we just produce repetative beeps
using the inbuilt speaker.
The left and right keys (5 and 8) change the tone.
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 98 ⟶ 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}}
 
[[Category:Electronics]]
[[Category:Sciences]]
[[Category:Temporal media]]
2,122

edits