Skip to content

Commit 9baf839

Browse files
committed
自适应画图窗口;模型使用开关~;文件选择适配。
1 parent e41a950 commit 9baf839

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

Audio/AudioDrawer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Drawer } from '../Drawer/Drawer.js';
22

33
class WaveDrawer extends Drawer {
4-
constructor(id = 'audioWave', width = 1000, height = 100, total_duration = 10, show_time = true) {
4+
constructor(id = 'audioWave', width = document.body.clientWidth * 0.8, height = 125, total_duration = 10, show_time = true) {
55
super(id, width, height);
66
this.total_duration = total_duration;
77
this.show_time = show_time;
@@ -35,7 +35,7 @@ class WaveDrawer extends Drawer {
3535
for (let i = 0; i < audio_canvas_length; i += 1) {
3636
const cur_x = i;
3737
for (let cp = 0; cp < perpixel_n && cp < audio_pcm.length - i * perpixel_n; cp += 1) {
38-
const cur_w = sin_one(audio_pcm[i * perpixel_n + cp]);
38+
const cur_w = (audio_pcm[i * perpixel_n + cp]);
3939
const cur_y = Math.round(cur_w * end_dy + end_y);
4040
const cur_n = flatten_wave_imgArray_count[cur_x + cur_y * audio_canvas_length] + 1;
4141
flatten_wave_imgArray_count[cur_x + cur_y * audio_canvas_length] = cur_n;
@@ -79,16 +79,16 @@ class WaveDrawer extends Drawer {
7979

8080
function sin_one(x) {
8181
return Math.sin(x * Math.PI / 2);
82-
}
82+
};
8383

8484
function circle_one(x) {
8585
if (x >= 0) return Math.sqrt(x * (2 - x));
8686
else return -Math.sqrt(-x * (2 - x));
87-
}
87+
};
8888

8989

9090
class StftDrawer extends Drawer {
91-
constructor(id = 'audioStft', width = 1000, height = null, total_duration = 10, show_time = true) {
91+
constructor(id = 'audioStft', width = document.body.clientWidth * 0.8, height = null, total_duration = 10, show_time = true) {
9292
super(id, width, height);
9393
this.total_duration = total_duration;
9494
this.adaptive_height = (!height);

Drawer/Drawer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Drawer {
2-
constructor(id = 'canvas', width = 600, height = 400) {
2+
constructor(id = 'canvas', width = document.body.clientWidth * 0.8, height = 400) {
33

44
this.canvas = document.querySelector(`canvas#${id}`);
55
if (!this.canvas) {

Workers/AudioProcesserWorker.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ importScripts("../tensorflowjs/[email protected]");
1717

1818
async function init_model() {
1919
const model = await tf.loadGraphModel('../tensorflowjs/tfjsModel/tfjs_savedModel/model.json');
20-
21-
// const len = Math.round(self.audioContainer.max_duration / self.audioContainer.hop_s);
22-
// model.predict(tf.zeros([1, len, 129]));
2320
return model;
2421
};
2522

main.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import { MyWorker } from './Workers/MyWorker.js';
77
// 添加页面元素
88
$('body').append(`
99
<div>
10-
<input type="file" id="audio_input" accept="audio/*" capture="microphone"/>
10+
<input type="file" id="audio_input" accept=".wav, .mp3, .flac, .aac, .m4a, .opus, .ogg" capture="microphone"/>
1111
<button id="record_btn" >Record</button>
1212
</div>`
1313
); const audio_input = document.querySelector('#audio_input'); const record_btn = document.querySelector('#record_btn');
1414
$('body').append(`<div id='audios'></div>`); const audios_div = document.querySelector('#audios');
1515

16-
const waveDrawer = new WaveDrawer('audioWave', 1000, 125);
17-
const stftDrawer = new StftDrawer('audioStft', 1000, null);
18-
16+
const waveDrawer = new WaveDrawer('audioWave');
17+
const stftDrawer = new StftDrawer('audioStft');
1918
$('body').append(`<button id='switch_btn'></button>`); const switch_btn = document.querySelector('#switch_btn');
19+
$('body').append(`<button id='open_model_btn'></button>`); const open_model_btn = document.querySelector('#open_model_btn');
2020
// 元素添加完毕
2121

2222
// 设置页面元素事件
@@ -60,9 +60,8 @@ record_btn.onclick = async (e) => {
6060
};
6161
};
6262

63-
switch_btn.disabled = true;
64-
switch_btn.textContent = "Loading..."
65-
switch_btn.onclick = async function (e) {
63+
switch_btn.textContent = "Start";
64+
switch_btn.onclick = function (e) {
6665
if (audioProcesser.isRunning()) {
6766
audioProcesser.stop();
6867
e.target.textContent = "Start";
@@ -73,6 +72,19 @@ switch_btn.onclick = async function (e) {
7372

7473
};
7574

75+
let is_open_model = false;
76+
open_model_btn.disabled = true;
77+
open_model_btn.textContent = "ModelLoading...";
78+
open_model_btn.onclick = function (e) {
79+
if (is_open_model) {
80+
is_open_model = false;
81+
e.target.textContent = "OpenModel";
82+
} else {
83+
is_open_model = true;
84+
e.target.textContent = "StopModel";
85+
};
86+
};
87+
7688

7789
//
7890

@@ -95,8 +107,8 @@ myWorker.reciveData('Event', (content) => {
95107
break;
96108
case 'inited':
97109
console.log('myWorkerScript Inited!');
98-
switch_btn.textContent = "Start";
99-
switch_btn.disabled = false;
110+
open_model_btn.textContent = "OpenModel";
111+
open_model_btn.disabled = false;
100112
break;
101113
default:
102114
console.error(`[MainThread]收到未知Event:${content}`);
@@ -109,7 +121,7 @@ const audioProcesser = new AudioFlowProcesser(
109121
'sound',
110122
sampleRate,
111123
undefined,
112-
256,
124+
512,
113125
1,
114126
(audioData_Clip) => {
115127
audioContainer.updateAudioDataClip(audioData_Clip);
@@ -120,21 +132,23 @@ const audioProcesser = new AudioFlowProcesser(
120132
audioContainer.updateStftDataClip(stftData_Clip);
121133
stftDrawer.set_data(audioContainer.getStftData());
122134

123-
myWorker.sendData(
124-
'stftData',
125-
{
126-
sampleRate: stftData_Clip.sampleRate,
127-
fft_n: stftData_Clip.fft_n,
128-
hop_n: stftData_Clip.hop_n,
129-
stft: {
130-
stftMartrixArrayBuffer: stftData_Clip.stft._arrayBuffer,
131-
stftMartrixHeight: stftData_Clip.stft.height,
132-
stftMartrixWidth: stftData_Clip.stft.width,
135+
if (is_open_model) {
136+
myWorker.sendData(
137+
'stftData',
138+
{
139+
sampleRate: stftData_Clip.sampleRate,
140+
fft_n: stftData_Clip.fft_n,
141+
hop_n: stftData_Clip.hop_n,
142+
stft: {
143+
stftMartrixArrayBuffer: stftData_Clip.stft._arrayBuffer,
144+
stftMartrixHeight: stftData_Clip.stft.height,
145+
stftMartrixWidth: stftData_Clip.stft.width,
146+
},
147+
audioTime: stftData_Clip.audioTime,
133148
},
134-
audioTime: stftData_Clip.audioTime,
135-
},
136-
[stftData_Clip.stft._arrayBuffer]
137-
);
149+
[stftData_Clip.stft._arrayBuffer]
150+
);
151+
};
138152
},
139153
null,
140154
);

0 commit comments

Comments
 (0)