Musical scale: Difference between revisions

From Rosetta Code
Content added Content deleted
m (ZX Spectrum Basic and Lilypond)
(→‎Tcl: Added implementation)
Line 1: Line 1:
{{draft task}}
{{draft task}}
[[Category:Temporal media]]

Output 8 notes of the C major natural diatonic music scale (These are the notes C,D,E,F,G,A,B,c or Do, Ra, Me, Fa, So, La, Te, do on the solfa) to the default musical sound device on the system. For the purpose of this task, middle C should be used as the starting note, and crotchet notes should be used.
Output 8 notes of the C major natural diatonic music scale (These are the notes C,D,E,F,G,A,B,c or Do, Ra, Me, Fa, So, La, Te, do on the solfa) to the default musical sound device on the system. For the purpose of this task, middle C should be used as the starting note, and crotchet notes should be used.


Line 6: Line 6:


=={{header|Lilypond}}==
=={{header|Lilypond}}==

The lilypond tool produces musical score sheets and does not output notes to the sound device.
The lilypond tool produces musical score sheets and does not output notes to the sound device.

<lang lilypond>% Start at middle C
<lang lilypond>% Start at middle C
\relative c' {
\relative c' {
Line 15: Line 13:
}</lang>
}</lang>


=={{header|ZX Spectrum Basic}}==
=={{header|Tcl}}==
{{libheader|Snack}}
<lang tcl>package require sound


# Encapsulate the tone generation
set filter [snack::filter generator 1 20000 0.5 sine -1]
set sound [snack::sound -rate 22050]
proc play {frequency length} {
global filter sound

$filter configure $frequency
$sound play -filter $filter

# Need to run event loop; Snack uses it internally
after $length {set donePlay 1}
vwait donePlay

$sound stop
}

# Major scale up, then down; extra delay at ends of scale
set tonicFrequency 261.63; # C4
foreach i {0 2 4 5 7 9 11 12 11 9 7 5 4 2 0} {
play [expr {$tonicFrequency*2**($i/12.0)}] [expr {$i%12?250:500}]
}</lang>

=={{header|ZX Spectrum Basic}}==
<lang zxbasic>10 REM Musical scale
<lang zxbasic>10 REM Musical scale
20 LET n=0: REM Start at middle C
20 LET n=0: REM Start at middle C
Line 27: Line 50:
90 STOP
90 STOP
9000 DATA 2,2,1,2,2,2,1,2:REM WWHWWWH</lang>
9000 DATA 2,2,1,2,2,2,1,2:REM WWHWWWH</lang>



{{omit from|BASIC}}
{{omit from|BASIC}}
Line 34: Line 56:
{{omit from|Openscad}}
{{omit from|Openscad}}
{{omit from|REXX}}
{{omit from|REXX}}

[[Category:Temporal media]]

Revision as of 14:27, 26 March 2013

Musical scale 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.

Output 8 notes of the C major natural diatonic music scale (These are the notes C,D,E,F,G,A,B,c or Do, Ra, Me, Fa, So, La, Te, do on the solfa) to the default musical sound device on the system. For the purpose of this task, middle C should be used as the starting note, and crotchet notes should be used.

For languages that cannot utilize a sound device, it is permissible to output to a musical score or the task can be omitted.

Lilypond

The lilypond tool produces musical score sheets and does not output notes to the sound device. <lang lilypond>% Start at middle C \relative c' {

 c d e f
 g a b c

}</lang>

Tcl

Library: Snack

<lang tcl>package require sound

  1. Encapsulate the tone generation

set filter [snack::filter generator 1 20000 0.5 sine -1] set sound [snack::sound -rate 22050] proc play {frequency length} {

   global filter sound
   $filter configure $frequency
   $sound play -filter $filter
   # Need to run event loop; Snack uses it internally
   after $length {set donePlay 1}
   vwait donePlay
   $sound stop

}

  1. Major scale up, then down; extra delay at ends of scale

set tonicFrequency 261.63; # C4 foreach i {0 2 4 5 7 9 11 12 11 9 7 5 4 2 0} {

   play [expr {$tonicFrequency*2**($i/12.0)}] [expr {$i%12?250:500}]

}</lang>

ZX Spectrum Basic

<lang zxbasic>10 REM Musical scale 20 LET n=0: REM Start at middle C 30 LET d=0.2: REM Make each note 0.2 seconds in duration 40 FOR l=1 TO 8 50 BEEP d,n 60 READ i: REM Number of semitones to increment 70 LET n=n+i 80 NEXT l 90 STOP 9000 DATA 2,2,1,2,2,2,1,2:REM WWHWWWH</lang>