clip.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. class Phone {
  2. constructor() {
  3. this.time = null;
  4. this.phoneme = null;
  5. }
  6. }
  7. class Clip {
  8. constructor() {
  9. this.audio = null; // data url for audio
  10. this.phones = [];
  11. this.indexedPhones = [];
  12. this.meanPitch = null; // Number
  13. this.meanResonance = null; // Number
  14. this.id = null;
  15. this.marker = null; // Element
  16. this.transcript = null;
  17. this.title = '';
  18. }
  19. loadAudioFile(file) {
  20. let reader = new FileReader(file);
  21. reader.readAsDataURL(file);
  22. reader.addEventListener("load", () => {
  23. this.audio = reader.result.replace(
  24. /data:.*;base64/,
  25. "data:audio/wav;base64"
  26. );
  27. this.id = hash(this.audio);
  28. });
  29. }
  30. loadResponse(data) {
  31. this.phones = data.phones;
  32. this.meanPitch = data.meanPitch;
  33. this.meanResonance = data.meanResonance;
  34. this.medianPitch = data.medianPitch;
  35. this.medianResonance = data.medianResonance;
  36. this.stdevPitch = data.stdevPitch;
  37. this.stdevResonance = data.stdevResonance;
  38. this.indexedPhones = Array(Math.ceil(last(this.phones).time * 100));
  39. for (let phone of data.phones) {
  40. this.indexedPhones[Math.floor(phone.time * 100)] = phone;
  41. }
  42. // Fill in missing values with what came before
  43. for (let i = 0; i < this.indexedPhones.length; i++) {
  44. if (!this.indexedPhones[i]) {
  45. if (this.indexedPhones[i - 1]) {
  46. this.indexedPhones[i] = this.indexedPhones[i - 1];
  47. } else {
  48. throw 'First data point is undefined';
  49. }
  50. }
  51. }
  52. }
  53. }