Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const record = require('node-record-lpcm16');
  2. var mysql = require('mysql');
  3.  
  4. var con = mysql.createConnection({
  5.   host: "localhost",
  6.   user: "edulux",
  7.   password: "edulux",
  8.   database: "edulux",
  9. });
  10.  
  11. con.connect(function(err) {
  12.   if (err) throw err;
  13.   console.log("Connected!");
  14. });
  15.  
  16.  
  17. // Imports the Google Cloud client library
  18. const speech = require('@google-cloud/speech');
  19.  
  20. // Creates a client
  21. const client = new speech.SpeechClient();
  22.  
  23. /**
  24.  * TODO(developer): Uncomment the following lines before running the sample.
  25.  */
  26. const encoding = 'LINEAR16';
  27. const sampleRateHertz = 16000;
  28. const languageCode = 'en-US';
  29.  
  30. const request = {
  31.   config: {
  32.     encoding: encoding,
  33.     sampleRateHertz: sampleRateHertz,
  34.     languageCode: languageCode,
  35.   },
  36.   interimResults: false, // If you want interim results, set this to true
  37. };
  38.  
  39. // Create a recognize stream
  40. const recognizeStream = client
  41.   .streamingRecognize(request)
  42.   .on('error', console.error)
  43.   .on('data', function(data){
  44.     console.log(data.results[0]);
  45.    
  46.   if (data.results[0] && data.results[0].alternatives[0]){
  47.   var sql = "INSERT INTO speechtotext (translation_text, data) VALUES (" + con.escape(data.results[0].alternatives[0].transcript) + " , 'testing')" ;
  48. //var values = [[''+connection.escape(data.results[0].alternatives[0].transcript), 'Navetz Test']];
  49.  
  50.   con.query(sql,function (err, result) {
  51.     if (err) throw err;
  52.     console.log("1 record inserted, ID: " + result.insertId);
  53.   });
  54.   }
  55.  
  56.     process.stdout.write(
  57.       data.results[0] && data.results[0].alternatives[0]
  58.         ? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
  59.         : `\n\nReached transcription time limit, press Ctrl+C\n`
  60.     )
  61.   });
  62.  
  63. // Start recording and send the microphone input to the Speech API
  64. record
  65.   .start({
  66.     sampleRateHertz: sampleRateHertz,
  67.     threshold: 0,
  68.     // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
  69.     verbose: false,
  70.     recordProgram: 'rec', // Try also "arecord" or "sox"
  71.     silence: '10.0',
  72.   })
  73.   .on('error', console.error)
  74.   .pipe(recognizeStream);
  75.  
  76. console.log('Listening, press Ctrl+C to stop.');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement