Sine wave: Difference between revisions
m
syntax highlighting fixup automation
(Corrected tag order) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10:
=={{header|AmigaBASIC}}==
<syntaxhighlight lang
The maximum allowed sound duration parameter in AmigaBASIC is 77 units. Since a second equals 18.2 sound units, this tone will only play for 4.23 seconds.
=={{header|BASIC256}}==
<syntaxhighlight lang
=={{header|C}}==
{{trans|Python}}
<
#include <math.h>
#include <stdlib.h>
Line 46:
putchar(v % 256);
}
}</
Test:
<
./csine 440 5 | play - # Now either pipe output into SoX to play
./csine 440 5 > test.au # or redirect output to a file.</
=={{header|Delphi}}==
Line 57:
{{libheader| Winapi.MMSystem}}
Copy of Andreas Rejbrand example found here [https://stackoverflow.com/questions/7742377/how-can-i-generate-continuous-tones-of-varying-frequencies].
<syntaxhighlight lang="delphi">
program Sine_wave;
Line 158:
end;
end;
end.</
=={{header|Emacs Lisp}}==
Line 164:
===8-bit samples===
<
"Play a sine wave for dur seconds."
(setq header (unibyte-string ; AU header:
Line 178:
(play-sound `(sound :data ,s)))
(play-sine 440 5)</
While the generated AU sound file is 16 bit, the samples themselves are 8 bit because only their high byte is set by the sine function. Therefore you will hear some faint hiss in the background due to the higher noise floor of 8-bit audio.
===16-bit samples===
This (slightly slower) version of the function creates proper 16-bit samples by setting both high and low bytes, resulting in less playback noise.
<
"Play a sine wave for dur seconds."
(setq header (unibyte-string ; AU header:
Line 201:
(play-sound `(sound :data ,s)))
(play-sine16 440 5)</
=={{header|FreeBASIC}}==
Line 208:
Original code programmed by Angelo Rosina
[https://www.freebasic.net/forum/viewtopic.php?t=12733]
<
_ASYNC = &h000001
_NODEFAULT = &h000002
Line 325:
'This is the base syntax, works exactly like in QB
'Sound Frequency, Duration , Volume, A, D, S, R, ModFreq, ModStart, ModAmplitude, MA, MD, MS, MR
</syntaxhighlight>
=={{header|Go}}==
Line 331:
<br>
Go lacks audio support in its standard library and, whilst there are third party packages that could be used, an easier approach is to invoke the SoX utility's 'play' command as was done in the second Kotlin example.
<
import (
Line 347:
fmt.Println(err)
}
}</
=={{header|J}}==
Approximately 1751 Hz (slightly flat A, two octaves above middle C), five seconds duration:
<
4 wavplay wavmake <.1e3*1 o.i.55e3
</syntaxhighlight>
For simplicity, we use rely on a default wav sample rate of 11khz. Also, for simplicity, we are taking the sine of integer values (0, 1, 2, 3, 4, 5, 6), which gives us slightly more than six samples per cycle. 1e3 here represents the amplitude of our wave, which is interpreted as a signed 16 bit integer. So we're about 30 decibels below max volume (a factor of 10 in amplitude is a difference of 20 decibels):
<
30.309 0</
=={{header|JavaScript}}==
<
let osc = ctx.createOscillator();
osc.frequency.setValueAtTime(440, ctx.currentTime);
osc.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + 5);</
=={{header|Julia}}==
PortAudio library version.
<
function paudio()
Line 388:
play(paudio(), 440.0, 5.0)
</syntaxhighlight>
=={{header|Kotlin}}==
===Using Java Sound API===
<
import javax.sound.sampled.AudioFormat
Line 422:
close()
}
}</
===Invoking SoX===
An easier approach invoking the SoX utility's 'play' command which has this stuff built-in. The following was tested on Ubuntu 16.04.
<
fun main(args:Array<String>) {
Line 436:
val proc = pb.start()
proc.waitFor()
}</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
=={{header|Nim}}==
Using Sox external player.
<
proc getIntValue(msg: string; minval, maxval: int): int =
Line 465:
let kind = "sine"
let args = ["-n", "synth", $duration, $kind, $freq]
echo execProcess("play", args = args, options = {poStdErrToStdOut, poUsePath})</
=={{header|OCaml}}==
Line 472:
{{libheader|ocaml-sfml}}
<
module BA1 = Bigarray.Array1
Line 499:
while true do
SFTime.sleep (SFTime.of_milliseconds 100_l);
done</
To run this code in script mode you can add this at the beginning of the file:
<
#load "bigarray.cma"
#load "sfml_system.cma"
#load "sfml_audio.cma"</
Then run:
<pre>ocaml sine_wave.ml</pre>
Line 516:
=={{header|Perl}}==
<
Audio::NoiseGen::init() || die 'No access to sound hardware?';
alarm 5;
play( gen => sine( freq => 440 ) );</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (dll/c_proc, system, prompt_number)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">k32</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xBeep</span>
Line 541:
<span style="color: #000000;">beep</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">prompt_number</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Enter Frequency (100..10000 recommended):"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0x25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0x7FFF</span><span style="color: #0000FF;">}))</span>
<!--</
=={{header|Processing}}==
Requires Processing Sound library.
<
import processing.sound.*;
Line 557:
delay(5000);
</syntaxhighlight>
=={{header|Python}}==
{{trans|Emacs Lisp}}
<
import os
Line 592:
os.system("vlc " + oname) # starts an external media player to play file
play_sine()</
Creates an AU file with the sine wave and plays it with VLC.
=={{header|Racket}}==
See https://docs.racket-lang.org/rsound/index.html
<syntaxhighlight lang="racket">
#lang racket
(require rsound)
; 440 Hz, 50% volume, 5 seconds
(play (make-tone 440 0.50 (* 5 FRAME-RATE)))
</syntaxhighlight>
=={{header|Raku}}==
Line 609:
What a horribly underspecified task. Ah well, gives me lots of wiggle room to cheat in various ways.
<syntaxhighlight lang="raku"
my $v = floor $rows / 2;
print "\e[H\e[J", 'Generating sine wave of zero amplitude and zero frequency for 5 seconds...',
Line 632:
say 'Oops, too late.';
say 'Still no? Ok how about:';
shell 'play -n -c1 synth 5.0 sin %-12';</
=={{header|REXX}}==
Note: This REXX program will <u>only</u> work for PC/REXX or Personal REXX.
<
parse arg freq time . /*obtain optional arguments from the CL*/
if freq=='' | freq=="," then freq= 880 /*Not specified? Then use the default.*/
if time=='' | time=="," then time= 5 /* " " " " " " */
call sound freq, time /*invoke a BIF to generate a sine wave.*/
exit 0 /*stick a fork in it, we're all done. */</
=={{header|Scala}}==
<
import scala.math.{Pi, sin}
Line 668:
line.close()
}</
=={{header|Wren}}==
Line 674:
{{libheader|Wren-sound}}
As Wren-cli doesn't have any built-in audio support, we instead build a .wav file which can then be played using a utility such as rhythmbox or SoX,
<
var sineWave = Fn.new { |frequency, seconds, sampleRate|
Line 690:
var sampleRate = 44000
var buffer = sineWave.call(440, 5, sampleRate)
Wav.create("sinewave.wav", buffer, sampleRate)</
=={{header|Yabasic}}==
<
// by Galileo, 05/2022
Line 708:
end sub
MyBeep(440)</
=={{header|zkl}}==
{{trans|Go}}
Running on Mint Linux
<
synthType:="sine";
cmd:="play -n synth %d %s %d".fmt(durationInSec,synthType,frequency);
Line 720:
}
sineWave();</
|