Sine wave: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 5: Line 5:
Generate a sine wave:
Generate a sine wave:


::# you choose the frequency of the wave
::# generate a sine wave for 5 seconds
::# generate a sine wave for 5 seconds
::# play sound
::# play sound

::# you choose the frequency of the wave





Revision as of 01:24, 21 May 2018

Sine wave is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Task

Generate a sine wave:

  1. you choose the frequency of the wave
  2. generate a sine wave for 5 seconds
  3. play sound


Perl 6

Works with: Rakudo version 2018.04.01

What a horribly underspecified task. Ah well, gives me lots of wiggle room to cheat in various ways.

<lang perl6>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...',

 "\e[$v;0H", '_' x $cols;

sleep 5; say "\e[H\e[J", 'No?, ok how about this:';

use SVG; my $filename = 'sine.svg'; my $out = open($filename, :w) or die "$!\n"; $out.say: SVG.serialize(

   svg => [
       width => 400, height => 150, style => 'stroke:rgb(0,0,255)',
       :rect[:width<100%>, :height<100%>, :fill<white>],
       :path[ :fill<none>, :d('M0,25 C36.42,25,63.58,125,100,125 M100,125 C136.42,125,163.58,25,200,25 M200,25 C236.42,25,263.58,125,300,125 M300,125 C336.42,125,363.58,25,400,25') ],
   ],

); close $out; say "Sine wave generated to {$filename.IO.absolute}, better open it quickly..."; sleep 5; unlink $filename; say 'Oops, too late.'; say 'Still no? Ok how about:'; shell 'play -n -c1 synth 5.0 sin %-12';</lang>