<commit_before>var tessel = require('tessel');
var fs = require('fs');
var song = fs.readFileSync('/app/sample.mp3');
var audio = require('./').use(tessel.port['A']);
var Readable = require('stream').Readable;
var filename = process.argv[2] || 'audio-recording.ogg';
var datas = [];

audio.on('data', function weRecorded(data) {
  console.log('got this recording data!', data.length);;
  datas.push(data);
})

audio.on('error', function(err) {
  console.log("ERROR:", err);
})

audio.on('startRecording', function() {
  console.log('started recording!');
});


audio.on('stopRecording', function() {
  console.log('stopped recording!');
  var rec = Buffer.concat(datas);
  console.log('playing len', rec.length);
  process.sendfile(filename, rec);
});


function testSwitchPlayRecord() {
  console.log('playing song.');
  audio.play(song, function(err) {
    if (err) return console.log("err playing..", err);
    else {
      console.log('finished playing...');
      audio.startRecording();
    }
  })
}

function testSwitchRecordPlay() {
  console.log('recording song.');
  audio.startRecording(function recStarted(err) {
    if (err) return console.log("err starting recording..", err);
  });
  setTimeout(function() {
    console.log('calling stop recording...');
    audio.stopRecording(function recStopped(err) {
      if (err) return console.log("err stopping recording..", err);
      else {
        audio.play(song);
      }
    })
  }, 3000);
}

function testRecording() {
  audio.startRecording('hifi-voice', function() {
    setTimeout(function stopRecording() {
      audio.stopRecording(function stopped() {
        console.log("Stop recording callback called...");
      })
    }, 4000);
  });
}

function testPlayback() {
  console.log('setting volume...');
  audio.setVolume(20, 20, function(err) {
    if (err) return console.log('err setting volume', err);
    console.log('volume set.');
    
    console.log('file read. Beginning to play...', song.length);  
    audio.play(song, function(err) {
      if (err) {
        console.log("error playing song: ", err);
      }
      else {
        console.log("Done playing the song");
      }
    });
  });
}

function testQueue() {
  console.log('testing queue');
  audio.setVolume(20, 20, function(e) {
    audio.play(song);
    audio.queue(song);
    audio.queue(song);
    audio.queue(song);
    audio.queue(song);
  });
}

function testPlayStream() {
  var rs = new Readable;
  var chunk = song.length/12;
  var incr = Math.floor(song.length/chunk);
  console.log('length', song.length, 'floor', Math.floor(song.length/chunk));

  
  for (var i = 0; i < incr; i++) {
    var pos = chunk * i;
    console.log('pos', pos);
    rs.push(song.slice(pos, pos + chunk));

  }

  if (song.length%chunk) {
    var pos = chunk * incr;
    console.log('last', pos, 'to', song.length%chunk);
    rs.push(song.slice(pos, pos + song.length%chunk));
  }
  // rs.push(song);
  rs.push(null);

  rs.pipe(audio.createPlayStream());
}

function testRecordStream() {
  var Writable = require('stream').Writable;
  var ws = Writable();
  ws._write = function (chunk, enc, next) {
      console.log("Write Stream Chunk: ", chunk);
      next();
  };

  audio.createRecordStream().pipe(ws);
}


audio.on('ready', function() {
  console.log("Ready to go!");
  // testRecordStream();
  // testPlayStream();
  // testSwitchPlayRecord();
  // testSwitchRecordPlay();
  // testRecording();
  // testPlayback();
  // testQueue();
  
});<commit_msg>Improves play stream test<commit_after>var tessel = require('tessel');
var fs = require('fs');
var song = fs.readFileSync('/app/sample.mp3');
var audio = require('./').use(tessel.port['A']);
var Readable = require('stream').Readable;
var filename = process.argv[2] || 'audio-recording.ogg';
var datas = [];

console.log('length should be', song.length);

audio.on('data', function weRecorded(data) {
  console.log('got this recording data!', data.length);;
  datas.push(data);
})

audio.on('error', function(err) {
  console.log("ERROR:", err);
})

audio.on('startRecording', function() {
  console.log('started recording!');
});


audio.on('stopRecording', function() {
  console.log('stopped recording!');
  var rec = Buffer.concat(datas);
  console.log('playing len', rec.length);
  process.sendfile(filename, rec);
});


function testSwitchPlayRecord() {
  console.log('playing song.');
  audio.play(song, function(err) {
    if (err) return console.log("err playing..", err);
    else {
      console.log('finished playing...');
      audio.startRecording();
    }
  })
}

function testSwitchRecordPlay() {
  console.log('recording song.');
  audio.startRecording(function recStarted(err) {
    if (err) return console.log("err starting recording..", err);
  });
  setTimeout(function() {
    console.log('calling stop recording...');
    audio.stopRecording(function recStopped(err) {
      if (err) return console.log("err stopping recording..", err);
      else {
        audio.play(song);
      }
    })
  }, 3000);
}

function testRecording() {
  audio.startRecording('hifi-voice', function() {
    setTimeout(function stopRecording() {
      audio.stopRecording(function stopped() {
        console.log("Stop recording callback called...");
      })
    }, 4000);
  });
}

function testPlayback() {
  console.log('setting volume...');
  audio.setVolume(20, 20, function(err) {
    if (err) return console.log('err setting volume', err);
    console.log('volume set.');
    
    console.log('file read. Beginning to play...', song.length);  
    audio.play(song, function(err) {
      if (err) {
        console.log("error playing song: ", err);
      }
      else {
        console.log("Done playing the song");
      }
    });
  });
}

function testQueue() {
  console.log('testing queue');
  audio.setVolume(20, 20, function(e) {
    audio.play(song);
    audio.queue(song);
    audio.queue(song);
    audio.queue(song);
    audio.queue(song);
  });
}

function testPlayStream() {
  var file = fs.createReadStream('/app/sample.mp3');
  file.pipe(audio.createPlayStream());
}

function testRecordStream() {
  var Writable = require('stream').Writable;
  var ws = Writable();
  ws._write = function (chunk, enc, next) {
      console.log("Write Stream Chunk: ", chunk);
      next();
  };

  audio.createRecordStream().pipe(ws);

  // setTimeout(function() {
  //   console.log('STOPPING');
  //   audio.stopRecording();
  // }, 2000);
}


audio.on('ready', function() {
  console.log("Ready to go!");
  testRecordStream();
  // testPlayStream();
  // testSwitchPlayRecord();
  // testSwitchRecordPlay();
  // testRecording();
  // testPlayback();
  // testQueue();
  
});
{'fraction_non_alphanumeric': 0.18369793227449804, 'fraction_numerical': 0.006592747977225052, 'mean_word_length': 3.4440745672436752, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3964397', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2134, 'n_words': 437}