欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kaidi-wasm学习笔记(二)重构promise

程序员文章站 2022-07-08 16:54:52
...
kaldi-wasm/src/workers/asrWorker.js
import JSZip from 'jszip';

import kaldiJS from '../computations/kaldiJS';
import kaldiWasm from '../computations/kaldiJS.wasm';
import KaldiConfigParser from '../utils/kaldiConfigParser';

const kaldiModule = kaldiJS({
  locateFile(path) {
    if (path.endsWith('.wasm')){
        console.log("hao-asrWorer.js---kaldiJS:"+path);
        return kaldiWasm;
    }
   // if (path.endsWith('.wasm')) return kaldiJS;
    return path;
  },
});

const MODEL_STORE = {
  NAME: 'models',
  KEY_PATH: 'language',
};

let asr = null;
let parser = null;

function mkdirExistOK(fileSystem, path) {
  console.log("hao:asrWorker.js---mkdirExistOK----->path:" +path)
//  console.log(fileSystem);

  try {
    //fileSystem.mkdir(path);
    fileSystem.mkdir(path);
  } catch (e) {
    console.log("hao--mkdirExistOK--error..$$$$$$$$$$$$$$$$$$$$");
    if (e.code !== 'EEXIST') throw e;
  }
}

function initEMFS(fileSystem, modelName) {
  console.log("hao-asrWorker---initEMFS--fileSystem:");
  console.log(fileSystem);
  mkdirExistOK(fileSystem, MODEL_STORE.NAME);
  console.log("hao-asrWorker---initEMFS--MODEL_STORE.NAME:"+MODEL_STORE.NAME);
  fileSystem.mount(fileSystem.filesystems.IDBFS, {},
    MODEL_STORE.NAME);
  fileSystem.chdir(MODEL_STORE.NAME);
  fileSystem.mkdir(modelName);
  fileSystem.chdir(modelName);
  console.log("hao-asrWorker---initEMFS--over.");
}

async function unzip(zipfile) {
//  console.log("hao-asrWorker---unzip--:");
//  console.log(zipfile);
  const zip = new JSZip();

  const unzipped = await zip.loadAsync(zipfile);
  return unzipped;
}

function dirname(path) {
  const dirs = path.match(/.*\//);
  if (dirs === null) return '';
  // without trailing '/'
  return dirs[0].slice(0, dirs[0].length - 1);
}

function mkdirp(fileSystem, path) {
  console.log("hao-asrWorker-----mkdirp--path:"+path);
  console.log(fileSystem);
  const dirBoundary = '/';
  const startIndex = path[0] === dirBoundary ? 1 : 0;
  for (let i = startIndex; i < path.length; i += 1) {
    if (path[i] === dirBoundary) mkdirExistOK(fileSystem, path.slice(0, i));
  }
  mkdirExistOK(fileSystem, path);
}

async function writeToFileSystem(fileSystem, path, fileObj) {
  console.log("asrWorker.js---writeToFileSystem---fileSystem:"+path);
  console.log(fileSystem);
  const content = await fileObj.async('arraybuffer');
  //console.log("content:"+content);
  try {
    fileSystem.writeFile(path, new Uint8Array(content));
    //fileSystem.writeFile(path, new Uint8Array(content),function(err){
    //    console.log("writefile-----error:"+err);
    //});
//    console.log("asrWorker.js---writeToFileSystem---writeFile----over.path:"+fileSystem.cwd()+"/"+path);
//    console.log("asrWorker.js---writeToFileSystem---isDir----models:"+fileSystem.isDir("/models"));
//    console.log("asrWorker.js---writeToFileSystem---isDir:::"+fileSystem.isDir(fileSystem.cwd()));
//    console.log("asrWorker.js---writeToFileSystem---final.mdl---isFile:"+fileSystem.isFile("final.mdl"));
    console.log("asrWorker.js---writeToFileSystem---end----------");
    return;
  } catch (e) {
    console.log("hao---error:-->>>>>>>>>>>>>>>>>>>>>>>writeToFileSystem......>path--->"+path);
    console.log(e);
    console.log(e.code);
    if (e.code === 'ENOENT') {
    //if (e.errno == 44) {
      const dirName = dirname(path);

      console.log("hao---error:-->dirname--writeToFileSystem......>"+dirName);
      mkdirp(fileSystem, dirName);
      // eslint-disable-next-line consistent-return
      return writeToFileSystem(fileSystem, path, fileObj);
    }
    throw e;
  }
}

var thisModule;
async function loadToFS(modelName, zip) {
//  console.log("hao-asrWorker---loadToFS--begin----kaldiModule:");
//  console.log(kaldiModule);
  console.log("hao-asrWorker---loadToFS---unzip begin")
  const unzipped = await unzip(zip);
  console.log("hao-asrWorker---loadToFS--unzip over....");

  await  kaldiModule.then(
    function(result){
       console.log("hao---hao-asrWorker---loadToFS----kaldiModule.then:")
       console.log(result.FS);
       thisModule=result;
       initEMFS(thisModule.FS, modelName);
   });
  //initEMFS(kaldiModule.FS, modelName);

  //const unzipped = await unzip(zip);
  //const unzipped = unzip(zip);
  // hack to wait for model saving on Emscripten fileSystem
  // unzipped.forEach does not allow to wait for end of async calls
  const files = Object.keys(unzipped.files);
  await Promise.all(
      files.map(async (file) => {
        console.log("asrWorker----loadToFS---Promise.all...files.map--->"+file);
        const content = unzipped.file(file);
        if (content !== null) {
          //await writeToFileSystem(kaldiModule.FS, content.name, content);
          //await writeToFileSystem(thisModule.FS, content.name, content);
//          console.log(" hao -----asrWorker----content.name--->"+content.name);
          const cwd = thisModule.FS.cwd();
//          console.log(" hao------asrWorker-----cwd------>"+cwd);
          await writeToFileSystem(thisModule.FS, content.name, content);
        }
      })
  );

  //  }
 // );
   // .then(
   // function(endResult){
   //     asr = startASR(endResult);
   //    // asr = startASR();
   // }
   // );
//  asr = startASR(thisModule);
  console.log("asrWorker----loadToFS---end-----------------------<");
  return true;
}

/*
 * Assumes that we are in the directory with the requested model
 */
//function startASR() {
//  console.log("hao-asrWorker---startASR");
//  parser = new KaldiConfigParser(kaldiModule.FS, kaldiModule.FS.cwd());
//  const args = parser.createArgs();
//  const cppArgs = args.reduce((wasmArgs, arg) => {
//    wasmArgs.push_back(arg);
//    return wasmArgs;
//  }, new kaldiModule.StringList());
//  return new kaldiModule.OnlineASR(cppArgs);
//}
//function startASR(asrModule) {
function startASR() {
  console.log("hao-asrWorker---startASR---------->thisModule.FS:");
  console.log(thisModule.FS);
  //parser = new KaldiConfigParser(kaldiModule.FS, kaldiModule.FS.cwd());
  parser = new KaldiConfigParser(thisModule.FS, thisModule.FS.cwd());
  console.log("hao-asrWorker---startASR--------------------------> cwd");
  const args = parser.createArgs();
  const cppArgs = args.reduce((wasmArgs, arg) => {
    wasmArgs.push_back(arg);
    return wasmArgs;
  },
    //new kaldiModule.StringList());
    new thisModule.StringList());

  //return new kaldiModule.OnlineASR(cppArgs);
  return new thisModule.OnlineASR(cppArgs);
}

const helper = {
  async init(msg) {
    console.log("hao-asrWoker---init:"+msg+",msg:");
    console.log(msg);
    await loadToFS(msg.data.modelName, msg.data.zip);
    asr = startASR();
    //asr = startASR(thisModule);
  },
  async process(msg) {
    console.log("hao--->asrWorker--->process---msg:");
    console.log(msg);
    if (asr === null) throw new Error('ASR not ready');
    const asrOutput = asr.processBuffer(msg.data.pcm);//pcm undefined
	console.log("hao--->asrWorker--->process:"+asrOutput);
    if (asrOutput === ''){
		console.log("hao:asrWorker.js ##############error this will null!!!!!!");
		return null;
	}
    return {
      isFinal: asrOutput.endsWith('\n'),
      text: asrOutput.trim(),
    };
  },
  async samplerate() {
    console.log("hao--->asrWorker---->samplerate:");
	console.log("parser:");
	console.log(parser);
    if (parser === null) throw new Error('ASR not ready');
	console.log(parser.getSampleRate());
    return parser.getSampleRate();
  },
  async reset() {
    if (asr === null) throw new Error('ASR not ready');
    const asrOutput = asr.reset();
    const result = {
      isFinal: asrOutput.endsWith('\n'),
      text: asrOutput.trim(),
    };
    return result;
  },
  async terminate() {
    if (asr !== null) asr.delete();
    asr = null;
  },
};

onmessage = (msg) => {
  const { command } = msg.data;
  const response = { command, ok: true };

  if (command in helper) {
    helper[command](msg)
      .then((value) => { response.value = value; })
      .catch((e) => {
        response.ok = false;
        response.value = e;
      })
      .finally(() => { postMessage(response); });
  } else {
    response.ok = false;
    response.value = new Error(`Unknown command '${command}'`);
    postMessage(response);
  }
};


resamplerWorker.js

/* eslint-disable no-restricted-globals */
import resampleJS from '../computations/resampleTo16bint';
import resampleWasm from '../computations/resampleTo16bint.wasm';

let resample = () => {};
let outputInputSampleRateRatio = 1 / 3;

/* Webpack renames resources which makes the locateFile function inside
resampleJS break. The function below replaces locateFile so as to give the
right name when loading the wasm binary.
*/
const resampleMod = resampleJS({
  locateFile(path) {
	console.log("hao---------->resamplerWorker.js----------->locateFile resampleMod--->>>>>>");
    if (path.endsWith('.wasm')) return resampleWasm;
    return path;
  },
});
var thisresampleMod;
const helper = {
  //setConversionRatio(msg) {
  async setConversionRatio(msg) {
	await  resampleMod.then(
	    function(result){
	       console.log("hao--->>>>>resamplerWorker.js----thisresampleMod.then>>>>>>")
	       thisresampleMod=result;
		    thisresampleMod.init();
		//  resample = resampleMod.resampleTo16bint;
			resample = thisresampleMod.resampleTo16bint;
	       //initEMFS(thisModule.FS, modelName);
	    }
    );

	console.log("hao---------->resamplerWorker.js----------->helper-->setConversionRatio msg:");
	console.log(msg);
    outputInputSampleRateRatio = msg.data.conversionRatio;
    return outputInputSampleRateRatio;
  },
  async resample(msg) {
	console.log("hao---------->resamplerWorker.js----------->helper-->resample ,msg:");
	console.log(msg);
    return resample(msg.data.buffer, outputInputSampleRateRatio);
  },
  async reset() {
//    resampleMod.reset();
	thisresampleMod.reset();
    return '';
  },
  async terminate() {
	console.log("hao---------->resamplerWorker.js----------->helper-->terminate >>");
    //resampleMod.terminate();
    thisresampleMod.terminate();
    close();
    return '';
  },
};

onmessage = (msg) => {
  const { command } = msg.data;
  const response = { command, ok: true };
  if (command in helper) {
    helper[command](msg)
      .then((value) => { response.value = value; })
      .catch((e) => {
        response.ok = false;
        response.value = e;
      })
      .finally(() => { postMessage(response); });
  } else {
    response.ok = false;
    response.value = new Error(`Unknown command '${command}'`);
    postMessage(response);
  }
//  if (command in helper){
//		console.log("hao---------->resamplerWorker.js--->onmessage command:"+command)
//		response.value = helper[command](msg);
//  }
//  else {
//    response.ok = false;
//    response.value = new Error(`Unknown command '${command}'`);
//  }
//  postMessage(response);
};

//resampleMod.onRuntimeInitialized = () => {
//  console.log("hao---->resamplerWorker.js-->>>resampleMod.onRuntimeInitialized");
//  resampleMod.init();
//  resample = resampleMod.resampleTo16bint;
//};



相关标签: kaldi