- Timestamp:
- 2021-09-21T14:58:07+12:00 (3 years ago)
- Location:
- main/trunk/model-interfaces-dev/atea/korero-maori-asr/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/App.vue
r35445 r35446 103 103 data() { 104 104 return { 105 /** @type {{id: String, url: String} | null} */106 currentlyLoadedAudio: null,107 player: new Audio(),108 105 showInfo: false 109 106 } … … 111 108 computed: mapState({ 112 109 translations: state => state.translations, 113 transcriptions: state => state.rawTranscriptions, 114 playbackState: state => state.playbackState, 115 shouldPlayAudio: state => state.playbackState.isPlaying 116 }), 117 watch: { 118 async playbackState(newValue) { 119 if (!newValue.isPlaying) { 120 return; 121 } 122 123 const transcription = this.transcriptions.get(newValue.id); 124 loadTranscriptionAudio(this.player, transcription, this.currentlyLoadedAudio); 125 126 let playbackTime = 0; 127 if (newValue.currentTime > 0) { 128 playbackTime = newValue.currentTime; 129 } 130 131 this.player.currentTime = playbackTime; 132 await this.player.play(); 133 134 this.playbackState.length = this.player.duration; 135 }//, 136 // shouldPlayAudio(newValue) { 137 // if (newValue && this.$refs.audioPlayer.paused) { 138 // this.$refs.audioPlayer.play(); 139 // } 140 // else if (!newValue && !this.$refs.audioPlayer.paused) { 141 // this.$refs.audioPlayer.pause(); 142 // } 143 // } 144 }, 145 mounted() { 146 pollAudioTime(this.player, this.$store); 147 } 148 } 149 150 // Adapted from https://davidwalsh.name/javascript-polling 151 function pollAudioTime(audioElement, store) { 152 var lastTime = 0; 153 (function p() { 154 if (audioElement.currentTime !== lastTime) { 155 lastTime = audioElement.currentTime; 156 store.commit("playbackStateSetCurrentTime", lastTime); 157 store.commit("playbackStateSetIsPlaying", true); 158 } 159 else { 160 store.commit("playbackStateSetIsPlaying", false); 161 } 162 163 setTimeout(p, 33); // Slightly more than 30hz 164 })(); 165 } 166 167 /** 168 * Loads an audio file. 169 * @param {Audio} player The audio player. 170 * @param {TranscriptionViewModel} transcription The name of the requested audio file. 171 * @param {{id: String, url: String} | null} current The currently loaded audio. 172 * @returns {{id: String, url: String}} If a new audio file was loaded, a new audio tracking object, else the current one. 173 */ 174 function loadTranscriptionAudio(player, transcription, current) { 175 if (current == null || current.id !== transcription.id) { 176 // TODO: Need to profile on some larger tracks; this may not be worth it? Better to cache a URL object instead? 177 if (current !== null) { 178 URL.revokeObjectURL(current.url); 179 } 180 181 const urlObject = URL.createObjectURL(transcription.file); 182 player.src = urlObject; 183 player.load(); 184 185 return { id: transcription.id, url: urlObject }; 186 } 187 188 return current; 110 transcriptions: state => state.rawTranscriptions 111 }) 189 112 } 190 113 </script> -
main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/components/TranscriptionItem.vue
r35445 r35446 3 3 <!-- Header containing info and actions for the transcription --> 4 4 <div class="transcription__header"> 5 <button class="btn-fab" v-on:click=" playAudio(-1)" type="button" title="Play Audio">5 <button class="btn-fab" v-on:click="toggleAudio" type="button" title="Play Audio"> 6 6 <span class="material-icons mdi-l play-button" v-if="!isPlaying">play_arrow</span> 7 7 <span class="material-icons mdi-l play-button" v-if="isPlaying">pause</span> … … 95 95 import { mapState } from "vuex"; 96 96 import { saveAs } from "file-saver" 97 import { TranscriptionViewModel } from "../main"; 98 import AudioPlayback from "../js/AudioPlaybackModule" 99 import Util from "../js/Util" 97 100 import TranscriptionItemEditor from "./TranscriptionItemEditor.vue" 98 import { TranscriptionViewModel, PlaybackState } from "../main";99 import Util from "../js/Util"100 101 101 102 export default { … … 115 116 computed: mapState({ 116 117 translations: state => state.translations, 117 isPlaying: state => state.playbackState.isPlaying 118 isPlaying(state) { 119 return state.playbackState.isPlaying && state.playbackState.id === this.transcription.id 120 } 118 121 }), 119 122 methods: { … … 121 124 this.showEditor = !this.showEditor; 122 125 }, 123 playAudio(startTime = 0) { 124 const pState = new PlaybackState(this.transcription.id, true, startTime); 125 this.$store.commit("setPlaybackState", pState); 126 toggleAudio() { 127 this.isPlaying ? AudioPlayback.pause() : AudioPlayback.play(this.transcription.id, -1); 126 128 }, 127 129 remove() { -
main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/components/TranscriptionItemEditor.vue
r35445 r35446 78 78 <script> 79 79 import { mapState } from "vuex"; 80 import { TranscriptionViewModel, PlaybackState } from "../main"; 80 import { TranscriptionViewModel } from "../main"; 81 import AudioPlayback from "../js/AudioPlaybackModule" 81 82 import AudioTimeBar from "./AudioTimeBar.vue" 82 83 import Util from "../js/Util"; … … 121 122 return { 122 123 enableEditing: false, 123 words: [] 124 words: [], 125 lastHighlightedWordIndex: 0 124 126 } 125 127 }, … … 140 142 methods: { 141 143 playAudio(startTime) { 142 const pState = new PlaybackState(this.transcription.id, true, startTime); 143 this.$store.commit("setPlaybackState", pState); 144 AudioPlayback.play(this.transcription.id, startTime); 144 145 }, 145 146 /** … … 247 248 watch: { 248 249 currentPlaybackTime(newValue) { 250 this.words[this.lastHighlightedWordIndex].shouldHighlight = false; 251 249 252 if (this.playbackState.id !== this.transcription.id) { 250 253 return; 251 254 } 252 255 253 for (const word of this.words) { 254 word.shouldHighlight = word.startTime < newValue && word.endTime > newValue; 256 for (let i = 0; i < this.words.length; i++) { 257 const word = this.words[i]; 258 259 if (word.startTime < newValue && word.endTime > newValue) { 260 word.shouldHighlight = true; 261 this.lastHighlightedWordIndex = i; 262 break; 263 } 255 264 } 256 265 } -
main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/js/AudioPlaybackModule.js
r35445 r35446 28 28 function pollAudioTime(audioElement, store) { 29 29 var lastTime = 0; 30 var shouldCommitNotPlaying = true;31 30 32 (function p () {31 (function poll() { 33 32 if (audioElement.currentTime !== lastTime) { 34 33 lastTime = audioElement.currentTime; 35 shouldCommitNotPlaying = true;36 37 34 store.commit("playbackStateSetCurrentTime", lastTime); 38 store.commit("playbackStateSetIsPlaying", true);39 }40 else if (shouldCommitNotPlaying) {41 store.commit("playbackStateSetIsPlaying", false);42 shouldCommitNotPlaying = false;43 35 } 44 36 45 setTimeout(p , 33); // Slightly more than 30hz37 setTimeout(poll, 33); // Slightly more than 30hz 46 38 })(); 47 39 } … … 79 71 this.loadedAudio = new LoadedAudio(null, null); 80 72 81 pollAudioTime(this.player, store) 73 pollAudioTime(this.player, store); 74 75 this.player.addEventListener("ended", function() { 76 store.commit("playbackStateSetIsPlaying", false); 77 }); 82 78 } 83 79 … … 88 84 */ 89 85 static async play(id, startTime = -1) { 90 if (this.loadedAudio.id !== id) {91 this.loadedAudio = loadAudio(this.player, this.store.state.rawTranscriptions[id], this.loadedAudio);92 93 this.store.commit("playbackStateSetID", id);94 }95 96 86 const playbackState = this.store.state.playbackState; 97 87 … … 101 91 } 102 92 93 if (this.loadedAudio.id !== id) { 94 this.loadedAudio = loadAudio(this.player, this.store.state.rawTranscriptions.get(id), this.loadedAudio); 95 96 this.store.commit("playbackStateSetID", id); 97 } 98 103 99 this.player.currentTime = playbackTime; 104 100 await this.player.play(); 101 102 this.store.commit("playbackStateSetLength", this.player.duration); 103 this.store.commit("playbackStateSetIsPlaying", true); 105 104 } 106 105 107 106 static pause() { 108 107 this.player.pause(); 108 this.store.commit("playbackStateSetIsPlaying", false); 109 109 } 110 110 } -
main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/main.js
r35445 r35446 104 104 state.playbackState.currentTime = time; 105 105 }, 106 playbackStateSetLength(state, length) { 107 state.playbackState.length = length; 108 }, 106 109 107 /**108 * Sets the current playback state.109 * @param {*} state The state of the store.110 * @param {PlaybackState} playbackState The new playback state.111 */112 setPlaybackState(state, playbackState) {113 state.playbackState = playbackState;114 },115 110 setTranslations(state, translations) { 116 111 state.translations = translations;
Note:
See TracChangeset
for help on using the changeset viewer.