Sine wave: Difference between revisions

1,838 bytes added ,  1 month ago
Added Uiua solution
(Sine wave in FreeBASIC)
(Added Uiua solution)
 
(7 intermediate revisions by 4 users not shown)
Line 10:
 
=={{header|AmigaBASIC}}==
<syntaxhighlight lang ="basic">SOUND 440,77</langsyntaxhighlight>
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 BASIC256="basic256">sound 440, 5000</langsyntaxhighlight>
 
=={{header|C}}==
{{trans|Python}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Line 46:
putchar(v % 256);
}
}</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang="bash">gcc -o csine csine.c -lm
./csine 440 5 | play - # Now either pipe output into SoX to play
./csine 440 5 > test.au # or redirect output to a file.</langsyntaxhighlight>
 
=={{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">
<lang Delphi>
program Sine_wave;
 
Line 158:
end;
end;
end.</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
Line 164:
 
===8-bit samples===
<langsyntaxhighlight lang="lisp">(defun play-sine (freq dur)
"Play a sine wave for dur seconds."
(setq header (unibyte-string ; AU header:
Line 178:
(play-sound `(sound :data ,s)))
 
(play-sine 440 5)</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="lisp">(defun play-sine16 (freq dur)
"Play a sine wave for dur seconds."
(setq header (unibyte-string ; AU header:
Line 201:
(play-sound `(sound :data ,s)))
 
(play-sine16 440 5)</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
Line 208:
Original code programmed by Angelo Rosina
[https://www.freebasic.net/forum/viewtopic.php?t=12733]
<langsyntaxhighlight lang="freebasic">Enum FLAGS
_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>
</lang>
 
=={{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 347:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|J}}==
Approximately 1751 Hz (slightly flat A, two octaves above middle C), five seconds duration:
<langsyntaxhighlight Jlang="j"> require'media/wav'
4 wavplay wavmake <.1e3*1 o.i.55e3
</syntaxhighlight>
</lang>
 
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):
 
<syntaxhighlight lang="j"> 20*10 ^. (2^15) % 1e3, 2^15
30.309 0</syntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">let ctx = new (window.AudioContext || window.webkitAudioContext)();
let osc = ctx.createOscillator();
osc.frequency.setValueAtTime(440, ctx.currentTime);
osc.connect(ctx.destination);
osc.start();
osc.stop(ctx.currentTime + 5);</langsyntaxhighlight>
 
=={{header|Julia}}==
PortAudio library version.
<langsyntaxhighlight lang="julia">using PortAudio
 
function paudio()
Line 383 ⟶ 388:
 
play(paudio(), 440.0, 5.0)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
===Using Java Sound API===
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
import javax.sound.sampled.AudioFormat
Line 417 ⟶ 422:
close()
}
}</langsyntaxhighlight>
 
===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.
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
fun main(args:Array<String>) {
Line 431 ⟶ 436:
val proc = pb.start()
proc.waitFor()
}</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">EmitSound[Play[Sin[440 2 Pi t], {t, 0, 5}]]</langsyntaxhighlight>
 
=={{header|Nim}}==
Using Sox external player.
<langsyntaxhighlight Nimlang="nim">import osproc, strutils
 
proc getIntValue(msg: string; minval, maxval: int): int =
Line 460 ⟶ 465:
let kind = "sine"
let args = ["-n", "synth", $duration, $kind, $freq]
echo execProcess("play", args = args, options = {poStdErrToStdOut, poUsePath})</langsyntaxhighlight>
 
=={{header|OCaml}}==
Line 467 ⟶ 472:
{{libheader|ocaml-sfml}}
 
<langsyntaxhighlight lang="ocaml">module BA = Bigarray
module BA1 = Bigarray.Array1
 
Line 494 ⟶ 499:
while true do
SFTime.sleep (SFTime.of_milliseconds 100_l);
done</langsyntaxhighlight>
 
To run this code in script mode you can add this at the beginning of the file:
 
<langsyntaxhighlight lang="ocaml">#directory "+sfml"
#load "bigarray.cma"
#load "sfml_system.cma"
#load "sfml_audio.cma"</langsyntaxhighlight>
Then run:
<pre>ocaml sine_wave.ml</pre>
Line 511 ⟶ 516:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use Audio::NoiseGen qw(play sine);
 
Audio::NoiseGen::init() || die 'No access to sound hardware?';
 
alarm 5;
play( gen => sine( freq => 440 ) );</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<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 536 ⟶ 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>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
Requires Processing Sound library.
<syntaxhighlight lang="java">
import processing.sound.*;
 
SinOsc sine;
 
size(500,500);
 
sine = new SinOsc(this);
sine.freq(500);
sine.play();
 
delay(5000);
</syntaxhighlight>
 
=={{header|Python}}==
{{trans|Emacs Lisp}}
<langsyntaxhighlight lang="python">#!/usr/bin/env python
 
import os
Line 571 ⟶ 592:
os.system("vlc " + oname) # starts an external media player to play file
 
play_sine()</langsyntaxhighlight>
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>
#lang racket
(require rsound)
; 440 Hz, 50% volume, 5 seconds
(play (make-tone 440 0.50 (* 5 FRAME-RATE)))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 588 ⟶ 609:
What a horribly underspecified task. Ah well, gives me lots of wiggle room to cheat in various ways.
 
<syntaxhighlight lang="raku" perl6line>my ($rows,$cols) = qx/stty size/.words;
my $v = floor $rows / 2;
print "\e[H\e[J", 'Generating sine wave of zero amplitude and zero frequency for 5 seconds...',
Line 611 ⟶ 632:
say 'Oops, too late.';
say 'Still no? Ok how about:';
shell 'play -n -c1 synth 5.0 sin %-12';</langsyntaxhighlight>
 
=={{header|REXX}}==
Note: &nbsp; This REXX program will <u>only</u> work for PC/REXX or Personal REXX.
<langsyntaxhighlight REXXlang="rexx">/*REXX program produces a sine wave (of a specified frequency) for N seconds. */
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. */</langsyntaxhighlight><br><br<
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import javax.sound.sampled.{AudioFormat, AudioSystem, SourceDataLine}
 
import scala.math.{Pi, sin}
Line 647 ⟶ 668:
line.close()
 
}</langsyntaxhighlight>
 
=={{header|Uiua}}==
{{works with|Uiua|0.11.0-dev.1}}
[https://www.uiua.org Uiua Pad] has built-in audio support for suitable arrays, so this will play a standard A for 5 seconds.
<syntaxhighlight lang="Uiua">
∿×τ×440×⟜(÷⟜⇡.×&asr) 5
</syntaxhighlight>
 
=={{header|Wren}}==
Line 653 ⟶ 681:
{{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,
<langsyntaxhighlight ecmascriptlang="wren">import "./sound" for Wav
 
var sineWave = Fn.new { |frequency, seconds, sampleRate|
Line 669 ⟶ 697:
var sampleRate = 44000
var buffer = sineWave.call(440, 5, sampleRate)
Wav.create("sinewave.wav", buffer, sampleRate)</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sine_wave
// by Galileo, 05/2022
 
Line 687 ⟶ 715:
end sub
 
MyBeep(440)</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Go}}
Running on Mint Linux
<langsyntaxhighlight lang="zkl">fcn sineWave(durationInSec=5, frequency=440){
synthType:="sine";
cmd:="play -n synth %d %s %d".fmt(durationInSec,synthType,frequency);
Line 699 ⟶ 727:
}
 
sineWave();</langsyntaxhighlight>
60

edits