// Morse tutor functions to send morse using soundmanager2
// see http://www.schillmania.com/content/projects/soundmanager2/

// It should be noted that soundmanager2 is distributed under a BSD
// license, the Nilex Morse Tutor is distributed under GPL v2.

//  Copyright 1998-2008 "Nosey" Nick Waterman, Ward Cunningham and Jim Wilson
//  Distributed under the GNU GPL V2 license.
//  See http://noseynick.net/va3nnw/cw/ and http://c2.com/morse
//
//  This file is part of the Nilex Morse Tutor.
//
//  The Nilex Morse Tutor is free software; you can redistribute it
//  and/or modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2 of
//  the License, or (at your option) any later version.
//
//  The Nilex Morse Tutor is distributed in the hope that it will be
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with the Nilex Morse Tutor: http://noseynick.net/va3nnw/cw/license.txt
//  If not, write to the Free Software Foundation, Inc., 59 Temple
//  Place, Suite 330, Boston, MA  02111-1307 USA

var Code = [
  0x0000010, 0x0000000, 0x0045D5D, 0x0000000, // ' ', '!', '"', '#'
  0x00475D5, 0x0000000, 0x0000000, 0x045DDDD, // '$', '%', '&', "'"
  0x0045DD7, 0x0475DD7, 0x0000000, 0x001175D, // '(', ')', '*', '+'
  0x0477577, 0x0047557, 0x011D75D, 0x0011757, // ',', '-', '.', '/'
  0x0477777, 0x011DDDD, 0x0047775, 0x0011DD5, // '0', '1', '2', '3'
  0x0004755, 0x0001155, 0x0004557, 0x0011577, // '4', '5', '6', '7'
  0x0045777, 0x0117777, 0x0115777, 0x01175D7, // '8', '9', ':', ';'
  0x0000000, 0x0011D57, 0x0000000, 0x0045775, // '<', '=', '>', '?'
  0x01175DD, 0x000011D, 0x0001157, 0x00045D7, // '@', 'A', 'B', 'C'
  0x0000457, 0x0000011, 0x0001175, 0x0001177, // 'D', 'E', 'F', 'G' 
  0x0000455, 0x0000045, 0x0011DDD, 0x00011D7, // 'H', 'I', 'J', 'K'
  0x000115D, 0x0000477, 0x0000117, 0x0004777, // 'L', 'M', 'N', 'O'
  0x00045DD, 0x0011D77, 0x000045D, 0x0000115, // 'P', 'Q', 'R', 'S'
  0x0000047, 0x0000475, 0x00011D5, 0x00011DD, // 'T', 'U', 'V', 'W'
  0x0004757, 0x0011DD7, 0x0004577,            // 'X', 'Y', 'Z'
];

var Morse = 0;
var cwso = null;
var ticker = null;

soundManager.debugMode = false;
soundManager.defaultOptions.autoLoad = true;
soundManager.allowPolling = false;
soundManager.onload = function() {
  debug("soundManager.onload()");
  chg_all(); // Set up everything else
}

function test_cq() {
  if (cwso) { cwso.destruct() }
  Morse = 0x175DC5D7; // CQ
  cwso = soundManager.createSound({
    id: freq,
    url: freq + 'hz.mp3',
    autoPlay: false,
    volume: 0,
    onload: start_cw
  });
}

function send_cw(letter) {
  sending();
  debug( "send_cw(" + letter + ")" );
  letter = letter.toUpperCase();
  
  if (" " <= letter && letter <= "Z")  {
    // If in Code[] table, convert it to Morse
    Morse = Code[letter.charCodeAt(0) - 32];
    start_cw();
  } else { // it's not morse
    Morse = 0;
  }
}

function start_cw() {
  debug("start_cw()");
  if (ticker) { window.clearInterval(ticker); ticker = null; }
  cwso.stop();
  cwso.setVolume(0);
  cwso.play();
  // wait 120ms for tone to begin playing before we modulate it into CW  :-(
  setTimeout("start_ticker()", 120);
}

// X WPM = X * "PARIS" / min
// PARIS = 50 dots total (inc spaces), so X WPM = X*50 DPM
// X*50 dots = 60000 msecs
// 1 dot = 1200 msecs / X WPM

function start_ticker() {
  debug("start_ticker()");
  ticker = window.setInterval('tick()', 1200 / speed);
}

function tick() {
  cwso.setVolume( vol * (Morse & 1));
  Morse = Morse >>> 1;
  sending();
  if (Morse > 1) { return; }
  
  window.clearInterval(ticker); ticker = null;
  cwso.stop();
  Morse = 0;
  done_sending();
}

function still_sending () {
  return (Morse>4);
}

