-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_analysis.m
773 lines (621 loc) · 25.6 KB
/
sound_analysis.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
function varargout = sound_analysis(varargin)
% SOUND_ANALYSIS MATLAB code for sound_analysis.fig
% SOUND_ANALYSIS, by itself, creates a new SOUND_ANALYSIS or raises the existing
% singleton*.
%
% H = SOUND_ANALYSIS returns the handle to a new SOUND_ANALYSIS or the handle to
% the existing singleton*.
%
% SOUND_ANALYSIS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SOUND_ANALYSIS.M with the given input arguments.
%
% SOUND_ANALYSIS('Property','Value',...) creates a new SOUND_ANALYSIS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before sound_analysis_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to sound_analysis_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help sound_analysis
% Last Modified by GUIDE v2.5 14-Oct-2021 18:32:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @sound_analysis_OpeningFcn, ...
'gui_OutputFcn', @sound_analysis_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before sound_analysis is made visible.
function sound_analysis_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to sound_analysis (see VARARGIN)
global flag1;flag1=0;
global flag2;flag2=0;
global sounding_flag;sounding_flag=0;
global listbox_string;
global myplayer;
global sound_data;
global ori_Fs;
% global sound_data;
wavlist = folder_search(pwd,'wav');
file_length = length(wavlist);
listbox_string = '';
fix_string = '';
for i = 1:1:file_length
listbox_string = [listbox_string, wavlist{i}, newline];
temp = strsplit(wavlist{i},'\');
fix_string = [fix_string, temp(end)];
end
% disp(listbox_string);
set(handles.listbox1,'string',fix_string);
% Choose default command line output for sound_analysis
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes sound_analysis wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = sound_analysis_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global flag1;
global myrecorder;
Fs=96000;
Bits=16;
Ch=2;
if flag1 == 0
myrecorder=audiorecorder(Fs,Bits,Ch);
record(myrecorder);
flag1 = 1;
set(handles.pushbutton1,'string','Finish');
else
stop(myrecorder);
fulldata = getaudiodata(myrecorder);
data=fulldata(:,1); %取单声道
n=0:length(data)-1; %信号等长序列
time=n/Fs; %时间轴对齐
axes(handles.axes1);
plot(time,data);
title('音频信号时域图')
xlabel('time/s');
ylabel('amplitude');
grid on;
N=length(data); %取信号长度
result=fft(data,N); %N点FFT变换
P=abs(result); %取模
frequence=n*Fs/N; %建立频率轴
axes(handles.axes2);
plot(frequence(1:fix(N/2)),P(1:fix(N/2)));
axis([0,4000,-inf,inf]);
title('音频信号频谱图');
xlabel('frequence/Hz');
ylabel('amplitude');
grid on;
[~,index]=max(data); %找声音峰值
timewin=floor(0.015*Fs); %提取峰值前后0.015s
xwin=data(index-timewin:index+timewin);
[y,~]=xcov(xwin);
ylen=length(y);
axes(handles.axes3);
plot(y);
fall_step=50;
halflen=(ylen+1)/2 +fall_step;
yy=y(halflen: ylen);
[~,maxindex] = max(yy);
fmax=Fs/(maxindex+30);
full_str=num2str(fmax);
cut_str=strsplit(full_str,'.');
text=[cut_str{1},'Hz; '];
if fmax<210
text=[text,'Male'];
else
text=[text,'Female'];
end
set(handles.text7,'string',text);
t = datestr(now);
fix_t = strrep(t(13:end),':','_');
name = ['myrecord_',fix_t,'.wav'];
audiowrite(name,data,Fs);
set(handles.edit2,'string',name);
wavlist = folder_search(pwd,'wav');
file_length = length(wavlist);
listbox_string = '';
fix_string = '';
for i = 1:1:file_length
listbox_string = [listbox_string, wavlist{i}, newline];
temp = strsplit(wavlist{i},'\');
fix_string = [fix_string, temp(end)];
if strcmp(temp(end),name)
set(handles.listbox1,'value',i);
end
end
set(handles.listbox1,'string',fix_string);
set(handles.pushbutton1,'string','Record');
set(handles.popupmenu2,'value',1);
flag1 = 0;
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global flag2;
global myplayer;
global sound_data;
global ori_Fs;
upend_value=get(handles.radiobutton1,'value');
if flag2 == 0
filename=get(handles.edit2,'string');
[sound_data,Fs]=audioread(filename);
if upend_value
sound_data = flip(sound_data);
end
ori_Fs=Fs;
% vocaltime=length(sound_data)/Fs;
slider_x=get(handles.slider3,'value');
speed=(slider_x+0.5).*(slider_x<0.5)+(2*slider_x).*(slider_x>=0.5);
fix_frequence=Fs*speed;
myplayer=audioplayer(sound_data,fix_frequence,16);
set(handles.pushbutton2,'string','Stop');
flag2 = 1;
play(myplayer);
else
stop(myplayer);
set(handles.pushbutton2,'string','Play');
flag2 = 0;
end
% function play_over_back(hObject, eventdata, handles)
% global myplayer;
% global flag2;
%
% while (isplaying(myplayer))
%
% end
%
% stop(myplayer);
% set(handles.pushbutton2,'string','Play');
% flag2 = 0;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global myplayer;
process = get(handles.slider1,'Value');
aim_start = round(process*myplayer.TotalSamples);
try %用try结构是怕在player暂未生成时用户移动滑动条而报错
pause(myplayer);
play(myplayer,aim_start);
% set(handles.togglebutton1,'String','l l','FontSize',15,'Value',1);
catch
set(handles.slider1,'Value',0);
end
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global myplayer;
global sound_data;
global ori_Fs;
state_value = get(handles.slider3,'Value');
speed = (state_value+0.5).*(state_value<0.5) + (2*state_value).*(state_value>=0.5);
vocal_value = get(handles.slider2,'Value');
amp = (2*vocal_value).*(vocal_value<=0.5) + (6*vocal_value-2).*(vocal_value>0.5);
try
aim_start = myplayer.CurrentSample;
fix_sound_data=amp*sound_data;
fix_frequence=speed*ori_Fs;
if isplaying(myplayer)
pause(myplayer);
myplayer=audioplayer(fix_sound_data,fix_frequence,16);
play(myplayer,aim_start);
else
myplayer=audioplayer(fix_sound_data,fix_frequence,16);
end
catch
end
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% sel=get(gcf,'selectiontype');
items = get(hObject,'String');
index_selected = get(hObject,'Value');
item_selected = items{index_selected};
set(handles.edit2,'string',item_selected);
% display([pwd,'\',item_selected]);
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
filename=get(handles.edit2,'string');
[vocal_data,Fs]=audioread(filename);
[~,index]=max(vocal_data); %找声音峰值
vocal_time=floor(0.015*Fs); %提取峰值前后0.015s
x=vocal_data(index-vocal_time:index+vocal_time);
[y,~]=xcov(x);
axes(handles.axes3);
plot(y);
% [~,y_index] = max(y);
% set(handles.edit5,'string',num2str(y_index));
y_len=length(y);
fall_step=50;
halflen=(y_len+1)/2 +fall_step;
y_half=y(halflen: y_len);
[~,maxindex] = max(y_half);
fmax=Fs/(maxindex+fall_step);
full_str=num2str(fmax);
cut_str=strsplit(full_str,'.');
text=[cut_str{1},'Hz; '];
% disp(['基音频率为 ', num2str(fmax), ' Hz'])
if fmax<210
text=[text,'Male'];
else
text=[text,'Female'];
end
set(handles.text7,'string',text);
n=0:length(vocal_data)-1; %信号等长序列
time=n/Fs; %时间轴对齐
axes(handles.axes1);
plot(time,vocal_data);
title('音频信号时域图')
xlabel('time/s');
ylabel('amplitude');
grid on;
N=length(vocal_data); %取信号长度
result=fft(vocal_data,N); %N点FFT变换
P=abs(result); %取模
frequence=n*Fs/N; %建立频率轴
axes(handles.axes2);
plot(frequence(1:fix(N/2)),P(1:fix(N/2)));
axis([0,4000,-inf,inf]);
title('音频信号频谱图');
xlabel('frequence/Hz');
ylabel('amplitude');
grid on;
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global myplayer;
global sound_data;
global ori_Fs;
state_value = get(handles.slider3,'Value');
speed = (state_value+0.5).*(state_value<0.5) + (2*state_value).*(state_value>=0.5);
vocal_value = get(handles.slider2,'Value');
amp = (2*vocal_value).*(vocal_value<=0.5) + (6*vocal_value-2).*(vocal_value>0.5);
try
aim_start = myplayer.CurrentSample;
fix_frequence=speed*ori_Fs;
fix_sound_data=amp*sound_data;
if isplaying(myplayer)
pause(myplayer);
myplayer=audioplayer(fix_sound_data,fix_frequence,16);
play(myplayer,aim_start);
else
myplayer=audioplayer(fix_sound_data,fix_frequence,16);
end
catch
end
% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global listbox_string;
filename=get(handles.edit2,'string');
delete(filename);
set(handles.listbox1,'value',1);
items = get(handles.popupmenu2,'string');
index_selected = get(handles.popupmenu2,'value');
filetype = items{index_selected};
filelist = folder_search(pwd,filetype);
file_length = length(filelist);
listbox_string = '';
fix_string = '';
for i = 1:1:file_length
listbox_string = [listbox_string, filelist{i}, newline];
temp = strsplit(filelist{i},'\');
fix_string = [fix_string, temp(end)];
end
set(handles.listbox1,'string',fix_string);
set(handles.edit2,'string','');
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
wavlist = folder_search(pwd,'wav');
file_length = length(wavlist);
for i = 1:1:file_length
delete(wavlist{i});
end
set(handles.listbox1,'string','');
set(handles.edit2,'string','');
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
global listbox_string;
items = get(handles.popupmenu2,'String');
index_selected = get(handles.popupmenu2,'Value');
item_selected = items{index_selected};
set(handles.listbox1,'value',1);
item_list = folder_search(pwd,item_selected);
file_length = length(item_list);
listbox_string = '';
fix_string = '';
for i = 1:1:file_length
listbox_string = [listbox_string, item_list{i}, newline];
temp = strsplit(item_list{i},'\');
fix_string = [fix_string, temp(end)];
end
set(handles.listbox1,'string',fix_string);
% set(handles.edit2,'string','');
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over slider3.
function slider3_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to slider3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.slider3,'value',0.5);
global myplayer;
global sound_data;
global ori_Fs;
try
aim_start = myplayer.CurrentSample;
fix_frequence=ori_Fs;
if isplaying(myplayer)
pause(myplayer);
myplayer=audioplayer(sound_data,fix_frequence,16);
play(myplayer,aim_start);
else
myplayer=audioplayer(sound_data,fix_frequence,16);
end
catch
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over slider2.
function slider2_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.slider2,'value',0.5);
global myplayer;
global sound_data;
global ori_Fs;
state_value = get(handles.slider3,'Value');
speed = (state_value+0.5).*(state_value<0.5) + (2*state_value).*(state_value>=0.5);
vocal_value = get(handles.slider2,'Value');
amp = (2*vocal_value).*(vocal_value<=0.5) + (6*vocal_value-2).*(vocal_value>0.5);
try
aim_start = myplayer.CurrentSample;
fix_sound_data=amp*sound_data;
fix_frequence=speed*ori_Fs;
if isplaying(myplayer)
pause(myplayer);
myplayer=audioplayer(fix_sound_data,fix_frequence,16);
play(myplayer,aim_start);
else
myplayer=audioplayer(fix_sound_data,fix_frequence,16);
end
catch
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over slider1.
function slider1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.slider1,'value',0);
global myplayer
try
pause(myplayer);
play(myplayer);
catch
set(handles.slider1,'Value',0);
end
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
function edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
aim_Fs=16000;
try
filename=get(handles.edit2,'string');
[sound_data,ori_Fs]=audioread(filename);
[P,Q] = rat(aim_Fs/ori_Fs);
resample_data=resample(sound_data,P,Q); %重采样
audiowrite('word_analysis.wav',resample_data,aim_Fs);
file_data = fopen('word_analysis.wav','rb');
byte_data = fread(file_data);
fclose(file_data);
delete('word_analysis.wav');
datalength=size(byte_data,1);
encoder = org.apache.commons.codec.binary.Base64;
json_data = char(encoder.encode(byte_data));
api_key = 'Fn40opF9VwBxyuWCm652gQQO';
secret_key = '8GivRan69sYnQuwHZCDiZwrxKZTKtg2j';
url_token = ['https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=',api_key,'&client_secret=',secret_key];
token = webread(url_token);
token = token.access_token ;
url = 'http://vop.baidu.com/server_api';
data = struct('format','wav','token',token,'len',datalength,...
'speech',json_data,'cuid','Sirius','rate',16000,'channel',1);
analysis = webwrite(url,data);
text = analysis.result{:};
set(handles.edit5,'string',text);
catch
end