<?
session_start();

ini_set(~display_errors~, true);
error_reporting(E_ALL^E_NOTICE);

$dbname=~ProjectManagement~;

include_once `../inc/report.php`;
include_once `../inc/styles.php`;

$UserId=$_SESSION[`UserId`];

$lineNumber=$_REQUEST[`refndx`];
if($lineNumber==``)
  {
    AlertCustom(~Error~,~No Scripture Reference Number~);
    die();
  }
else
  {
	$txt=ShowQueryResultScalar(~select text from Bible where line=$lineNumber~);
	TextClean($txt);
	$book=ShowQueryResultScalar(~select book from Bible where line=$lineNumber~);
	$chapter=ShowQueryResultScalar(~select chapter from Bible where line=$lineNumber~);
	$verse=ShowQueryResultScalar(~select verse from Bible where line=$lineNumber~);
	$ref=$book.` `.$chapter.`:`.$verse;
	$refm=$book.`_`.$chapter.`-`.$verse;
	$fil=~../Bible/audio/$book/~.$refm.~.wav~;
//echo ~fil=$fil<br>~;
  }

if($_FILES[`audio_data`])
  {
	$input = $_FILES[`audio_data`][`tmp_name`];
	$output = $fil;
	move_uploaded_file($input, $output);
  }

echo `

<!DOCTYPE html>

<html>
<head>
	<meta http-equiv=~Content-Type~ content=~text/html; charset=utf-8~>

<meta id=~meta~ name=~viewport~ content=~width=device-width; initial-scale=1.0~ />

	<title>Live input record and playback</title>
  <style type=~text/css~>
    ul { list-style: none; }
    #recordingslist audio { display: block; margin-bottom: 10px; }
  </style>
</head>
<body bgcolor=#F0FFFF>
<center>

<table border=0 width=100%><tr><td valign=top>

<a href=~javascript: window.close();~ title=~Close~><img src=../images/close.png width=30px></a>
</td><td width=80%>

  <h1>Audio for: <font color=green>`,$ref,`</font></h1>

</td></tr></table>

<table border=1 bgcolor=#FFFFEF cellpadding=10 width=75%><tr><td><h2>`,
$txt
,`</h2></td></tr></table>

<p>

  <div id=~recordingslist~></div>

`;

if ($UserId==12) echo `

<p>
  <button onclick=~startRecording(this);~>record</button>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <button onclick=~stopRecording(this);~ disabled>stop</button>
</center>
<p>

  <h2>Log</h2>
  <pre id=~log~></pre>
`;
echo `
  <script>

  var au = document.createElement(~audio~);
  au.controls = true;
  au.autoplay = true;
  au.src = ~`,$fil,`~;

  var li = document.createElement(~p~);
  li.appendChild(au);
  recordingslist.appendChild(li);
</script>
`;
if ($UserId==12) echo `
<script>
  function __log(e, data) {
    log.innerHTML += ~
~ + e + ~ ~ + (data || ~~);
  }

  var audio_context;
  var recorder;

  function startUserMedia(stream) {
    var input = audio_context.createMediaStreamSource(stream);
    __log(~Media stream created.~);

    // Uncomment if you want the audio to feedback directly
    //input.connect(audio_context.destination);
//    __log(~Input connected to audio context destination.~);

    recorder = new Recorder(input);
    __log(~Recorder initialised.~);
  }

  function startRecording(button) {
    recorder && recorder.record();
    button.disabled = true;
    button.nextElementSibling.disabled = false;
    __log(~Recording...~);
  }

  function stopRecording(button) {
    recorder && recorder.stop();
    button.disabled = true;
    button.previousElementSibling.disabled = false;
    __log(~Stopped recording.~);

    // create WAV download link using audio data blob
    createDownloadLink();

    recorder.clear();
  }

  function createDownloadLink() {
    recorder && recorder.exportWAV(function(blob) {
      var url = URL.createObjectURL(blob);
//      var li = document.createElement(~li~);
//      var au = document.createElement(~audio~);
//      var hf = document.createElement(~a~);
//      au.controls = true;
      au.src = url;
//      hf.href = url;
//      hf.download = new Date().toISOString() + ~.wav~;
//      hf.innerHTML = hf.download;
//      li.appendChild(au);
//      li.appendChild(hf);
//      recordingslist.appendChild(li);
//begin not example
var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
  if(this.readyState === 4) {
      console.log(~Server returned: ~,e.target.responseText);
  }
};
var fd=new FormData();
fd.append(~audio_data~,blob, ~filename~);
xhr.open(~POST~,~BibleAudio.php?refndx=`,$lineNumber,`~,true);
xhr.send(fd);
//end not example
    });
  }

  window.onload = function init() {
    try {
      // webkit shim
      window.AudioContext = window.AudioContext || window.webkitAudioContext;
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
      window.URL = window.URL || window.webkitURL;

      audio_context = new AudioContext;
      __log(~Audio context set up.~);
      __log(~navigator.getUserMedia ~ + (navigator.getUserMedia ? ~available.~ : ~not present!~));
    } catch (e) {
      alert(~No web audio support in this browser!~);
    }

    navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
      __log(~No live audio input: ~ + e);
    });
  };
  </script>

  <script src=~../../inc/Recorderjs-master/dist/recorder.js~></script>
`;
echo `
</body>
</html>

`;

?>