Back to main page
Back to sound contents

Experimenting with channels

This section shows how to make use of channels.

Since we want to play the sound and draw the waveform everytime we call the play function, we set some sound options defaults to avoid rewriting them.

set_sound_defaults(
   player    = "mplayer",
   draw_wave = true,
   draw_wave_options = [terminal   = png,
                        pic_height = 400,
                        pic_width  = 800] )$

Stereo. By default, waveforms are placed in channel #1, but it is possible to save waveforms in other channels. The graphical output (png format in this case) shows all available channels. This time, we call mplayer. You will hear two clics, one at t = 0.75, when the second channel starts to play, and the other at t = 1, when the first channel stops playing; this effect can be avoided by applying an appropiate envelope (see other examples below).

play(
   player = "mplayer",
   note(440,2000,1,0),
   channel = 2,
   note(440,2000,1,0.75)) $
Download wav file

According to Wikipedia, in acoustics, a beat is an interference between two sounds of slightly different frequencies, perceived as periodic variations in volume whose rate is the difference between the two frequencies.

Two notes (110.00 Hz and 103.82 Hz) in stereo during four seconds, both starting at t = 0.

play(
   note(note_freq("A",2),2000,4),
   channel = 2,
   note(note_freq("G#",2),2000,4)) $
Download wav file

Smooth transition from left to right and back.

play(
   envelope = pairs([0,1],[1,0]),
   note(440,2000,1),

   envelope = pairs([0,0],[1/2,1],[1,0]),
   channel = 2,
   note(440,2000,1.5,0.75),

   envelope = pairs([0,0],[1,1]),
   channel = 1,
   note(440,2000,1,2) ) $
Download wav file

Beethoven's 9th symphony for violin and clarinet.

sound_sample_rate: 2 * sound_sample_rate$ /* need more samples per sec */

DO5  : note_freq("C", 5) $
MI5  : note_freq("E", 5) $
SOL5 : note_freq("G", 5) $
DO6  : note_freq("C", 6) $
RE6  : note_freq("D", 6) $
MI6  : note_freq("E", 6) $
FA6  : note_freq("F", 6) $
SOL6 : note_freq("G", 6) $

play(
  
  /* the violin */
  channel = 1,
  envelope = adsr(0.4, 0.2, 0.6, 0.2),
  oscillator = triangle(0.99),  /* sawtooth */
  note(MI6, 2000, 0.5, 0, 0.5, 3, 5.5, 8, 8.5, 11, 13.5),
  note(MI6, 2000, 1, 6),
  note(FA6, 2000, 0.5, 1, 2.5, 9, 10.5),
  note(SOL6, 2000, 0.5, 1.5, 2, 9.5, 10),
  note(RE6, 2000, 0.5, 3.5, 5, 11.5, 13),
  note(RE6, 2000, 1, 7, 14),
  note(DO6, 2000, 0.5, 4, 4.5, 12, 12.5),
  note(DO6, 2000, 1, 15),

  /* the clarinet */
  channel=2,
  envelope = pairs([0,0],[0.05,1],[0.9,1],[1,0]),
  oscillator = sine(1, [3, 1/2], [5, 1/4], [7, 1/8]),
  note(DO6, 2000, 0.5, 0, 0.5, 3, 5.5, 8, 8.5, 11, 13.5),
  note(DO6, 2000, 1, 6),
  note(RE6, 2000, 0.5, 1, 2.5, 9, 10.5),
  note(MI6, 2000, 0.5, 1.5, 2, 9.5, 10),
  note(SOL5, 2000, 0.5, 3.5, 5, 11.5, 13),
  note(SOL5, 2000, 1, 7, 14),
  note(MI5, 2000, 0.5, 4, 4.5, 12, 12.5),
  note(DO5, 2000, 1, 15)    )$
Download wav file

Back to main page
Back to sound contents


by Mario Rodríguez Riotorto