Skip to content

Commit

Permalink
feat: Use a three-party library to convert audio to mp3
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuoFuhong committed Mar 23, 2023
1 parent f06dceb commit 8820b78
Show file tree
Hide file tree
Showing 4 changed files with 11,942 additions and 27 deletions.
4 changes: 2 additions & 2 deletions frontend/web-im/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="js/vue.js"></script>
<script src="js/recorder.mp3.min.js"></script>
<script src="lib/axios.min.js"></script>
<script src="js/config.js"></script>
<script src="js/axios.js"></script>
<script src="js/websocket.js"></script>
<script src="js/utils.js"></script>
<script src="js/audio.js"></script>
<script src="js/index.js"></script>
</body>
</html>
50 changes: 25 additions & 25 deletions frontend/web-im/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let app = new Vue({
messageCache: {},
wsclient: null,
seq: "0",
audioDevice: null,
recording: false
recording: false,
recorder: null
},
async created() {
let userId = getQueryVariable("uid")
Expand All @@ -33,40 +33,40 @@ let app = new Vue({
methods: {
// 初始化音频驱动
async initAudioDevice() {
const that = this
let audioDevice = new AudioRecorder()
audioDevice.getAudioRecorderDevice()
// 注册回调-音频转文本
audioDevice.addOnStopCallback(function(blob) {
var data = new FormData()
data.append('model', 'whisper-1')
data.append('file', blob, 'translate_tts.mp3')

const xhr = new XMLHttpRequest()
// xhr.withCredentials = true
xhr.addEventListener('readystatechange', function() {
if(this.readyState === 4) {
const rsp = JSON.parse(this.responseText)
// 发送转录的文本
that.wsClientSendToUser(rsp.text)
}
})
xhr.open('POST', config.BASE_URL + '/audio/transcriptions')
xhr.send(data)
var rec = Recorder();
rec.open(function(){
console.log("open audio device success.")
})
this.audioDevice = audioDevice;
this.recorder = rec;
},
// 录制音频
audioRecord() {
if (!this.recording) {
// 开始录制
console.log('start audio record')
this.audioDevice.startRecord()
this.recorder.start();
this.recording = true
} else {
// 停止录制
console.log('stop audio record')
this.audioDevice.stopRecord()
var that = this;
this.recorder.stop(function(blob,duration){
var data = new FormData()
data.append('model', 'whisper-1')
data.append('file', blob, 'translate_tts.mp3')

const xhr = new XMLHttpRequest()
// xhr.withCredentials = true
xhr.addEventListener('readystatechange', function() {
if(this.readyState === 4) {
const rsp = JSON.parse(this.responseText)
// 发送转录的文本
that.wsClientSendToUser(rsp.text)
}
})
xhr.open('POST', config.BASE_URL + '/audio/transcriptions')
xhr.send(data)
})
this.recording = false
}
},
Expand Down
6 changes: 6 additions & 0 deletions frontend/web-im/js/recorder.mp3.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 8820b78

Please sign in to comment.