PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/phonem.m

http://phonemexplorer.googlecode.com/
MATLAB | 552 lines | 313 code | 107 blank | 132 comment | 28 complexity | 95dff5afdef52fa1646f1bfdc2b6e878 MD5 | raw file
  1. function varargout = phonem(varargin)
  2. % PHONEM M-file for phonem.fig
  3. % PHONEM, by itself, creates a new PHONEM or raises the existing
  4. % singleton*.
  5. %
  6. % H = PHONEM returns the handle to a new PHONEM or the handle to
  7. % the existing singleton*.
  8. %
  9. % PHONEM('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in PHONEM.M with the given input arguments.
  11. %
  12. % PHONEM('Property','Value',...) creates a new PHONEM or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before phonem_OpeningFunction gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to phonem_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help phonem
  23. % Last Modified by GUIDE v2.5 03-Mar-2008 18:02:07
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @phonem_OpeningFcn, ...
  29. 'gui_OutputFcn', @phonem_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin & isstr(varargin{1})
  33. gui_State.gui_Callback = str2func(varargin{1});
  34. end
  35. if nargout
  36. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before phonem is made visible.
  42. function phonem_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to phonem (see VARARGIN)
  48. % Choose default command line output for phonem
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. global phonemes;
  53. global dbConnection;
  54. global isVocalized;
  55. global speakerID;
  56. global groupID;
  57. global isGroupPhonems;
  58. set(handles.rbtTypeVoc, 'Value', 1);
  59. set(handles.rbtSPM, 'Value', 1);
  60. set(handles.sldVolume, 'Value', 0.7);
  61. isVocalized = 1;
  62. groupID = [];
  63. isGroupPhonems = 0;
  64. if nargin > 3
  65. isGroupPhonems = varargin{1};
  66. if isGroupPhonems == 1
  67. groupID = varargin{2};
  68. groupInfo = getGroupInfo(dbConnection, groupID);
  69. set(handles.txtGroupName, 'String', groupInfo.GROUP_NAME);
  70. set(handles.txtSpeakerSex, 'String', ...
  71. getSexFromId(groupInfo.SPKR_GENDER));
  72. age = sprintf('%d-%d', groupInfo.MIN_AGE, groupInfo.MAX_AGE);
  73. set(handles.txtSpeakerAge, 'String', age);
  74. set(handles.txtSpeakerRegion, 'String', ...
  75. getRegionFromId(groupInfo.REGION_ID));
  76. names = getGroupPhonemNames(dbConnection, groupID);
  77. else
  78. speakerID = varargin{2};
  79. names = getSpeakerPhonemNames(dbConnection, speakerID);
  80. fillGroupInfo(handles);
  81. end
  82. else
  83. fillGroupInfo(handles);
  84. end
  85. for n = 1:length(phonemes)
  86. s = get(handles.lstPhonemes, 'String');
  87. if isGroupPhonems == 1
  88. name.PHONEM_NAME{1} = names.PHONEM_NAME{n};
  89. else
  90. name.PHONEM_NAME{1} = names.PHONEM_NAME{n};
  91. end
  92. if ~isequal(name.PHONEM_NAME{1}, 'null')
  93. s{length(s) + 1} = sprintf('?????? /%s/', name.PHONEM_NAME{1});
  94. else
  95. s{length(s) + 1} = sprintf('?????? %d', n);
  96. end
  97. set(handles.lstPhonemes, 'String', s);
  98. end
  99. updateAxes(1, handles);
  100. function fillGroupInfo(handles)
  101. set(handles.txtGroupName, 'String', '-');
  102. set(handles.txtSpeakerSex, 'String', '-');
  103. set(handles.txtSpeakerAge, 'String', '-');
  104. set(handles.txtSpeakerRegion, 'String', '-');
  105. % --- Outputs from this function are returned to the command line.
  106. function varargout = phonem_OutputFcn(hObject, eventdata, handles)
  107. % varargout cell array for returning output args (see VARARGOUT);
  108. % hObject handle to figure
  109. % eventdata reserved - to be defined in a future version of MATLAB
  110. % handles structure with handles and user data (see GUIDATA)
  111. % Get default command line output from handles structure
  112. varargout{1} = handles.output;
  113. % --- Executes during object creation, after setting all properties.
  114. function lstPhonemes_CreateFcn(hObject, eventdata, handles)
  115. % hObject handle to lstPhonemes (see GCBO)
  116. % eventdata reserved - to be defined in a future version of MATLAB
  117. % handles empty - handles not created until after all CreateFcns called
  118. % Hint: listbox controls usually have a white background on Windows.
  119. % See ISPC and COMPUTER.
  120. if ispc
  121. set(hObject,'BackgroundColor','white');
  122. else
  123. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  124. end
  125. % --- Executes on selection change in lstPhonemes.
  126. function lstPhonemes_Callback(hObject, eventdata, handles)
  127. % hObject handle to lstPhonemes (see GCBO)
  128. % eventdata reserved - to be defined in a future version of MATLAB
  129. % handles structure with handles and user data (see GUIDATA)
  130. index = get(hObject, 'Value');
  131. updateAxes(index, handles);
  132. % --- Executes on button press in rbtTypeVoc.
  133. function rbtTypeVoc_Callback(hObject, eventdata, handles)
  134. % hObject handle to rbtTypeVoc (see GCBO)
  135. % eventdata reserved - to be defined in a future version of MATLAB
  136. % handles structure with handles and user data (see GUIDATA)
  137. global isVocalized;
  138. isVocalized = 1;
  139. set(handles.rbtTypeNvoc, 'Value', 0);
  140. set(handles.rbtTypeVoc, 'Value', 1);
  141. index = get(handles.lstPhonemes, 'Value');
  142. updateAxes(index, handles);
  143. % --- Executes on button press in rbtTypeNvoc.
  144. function rbtTypeNvoc_Callback(hObject, eventdata, handles)
  145. % hObject handle to rbtTypeNvoc (see GCBO)
  146. % eventdata reserved - to be defined in a future version of MATLAB
  147. % handles structure with handles and user data (see GUIDATA)
  148. global isVocalized;
  149. isVocalized = 0;
  150. set(handles.rbtTypeVoc, 'Value', 0);
  151. set(handles.rbtTypeNvoc, 'Value', 1);
  152. index = get(handles.lstPhonemes, 'Value');
  153. updateAxes(index, handles);
  154. % --- Executes during object creation, after setting all properties.
  155. function txtF0Frequency_CreateFcn(hObject, eventdata, handles)
  156. % hObject handle to txtF0Frequency (see GCBO)
  157. % eventdata reserved - to be defined in a future version of MATLAB
  158. % handles empty - handles not created until after all CreateFcns called
  159. % Hint: edit controls usually have a white background on Windows.
  160. % See ISPC and COMPUTER.
  161. if ispc
  162. set(hObject,'BackgroundColor','white');
  163. else
  164. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  165. end
  166. % --- Executes during object creation, after setting all properties.
  167. function sldVolume_CreateFcn(hObject, eventdata, handles)
  168. % hObject handle to sldVolume (see GCBO)
  169. % eventdata reserved - to be defined in a future version of MATLAB
  170. % handles empty - handles not created until after all CreateFcns called
  171. % Hint: slider controls usually have a light gray background, change
  172. % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
  173. usewhitebg = 1;
  174. if usewhitebg
  175. set(hObject,'BackgroundColor',[.9 .9 .9]);
  176. else
  177. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  178. end
  179. % --- Executes on slider movement.
  180. function sldVolume_Callback(hObject, eventdata, handles)
  181. % hObject handle to sldVolume (see GCBO)
  182. % eventdata reserved - to be defined in a future version of MATLAB
  183. % handles structure with handles and user data (see GUIDATA)
  184. % Hints: get(hObject,'Value') returns position of slider
  185. % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
  186. % --- Executes on button press in btPhSynth.
  187. function btPhSynth_Callback(hObject, eventdata, handles)
  188. % hObject handle to btPhSynth (see GCBO)
  189. % eventdata reserved - to be defined in a future version of MATLAB
  190. % handles structure with handles and user data (see GUIDATA)
  191. duration = str2num(get(handles.txtPlayDuration, 'String'));
  192. global phonemes;
  193. global audioPlayer;
  194. global isVocalized;
  195. index = get(handles.lstPhonemes, 'Value');
  196. ar_model = [1 phonemes(index).AR];
  197. F0 = str2num(get(handles.txtF0Frequency, 'String'));
  198. Fs = 8000;
  199. volume = get(handles.sldVolume, 'Value');
  200. y = synthPhonem(ar_model, F0, Fs, duration * Fs, isVocalized);
  201. if isVocalized == 1
  202. y = y / max(abs(y)) * volume;
  203. else
  204. y = y / (3 * std(y)) * volume;
  205. end
  206. audioPlayer = audioplayer(y, Fs);
  207. set(audioPlayer, 'UserData', handles.btPhSynth);
  208. set(audioPlayer, 'StopFcn', @playingStoped);
  209. set(audioPlayer, 'StartFcn', @playingStarted);
  210. play(audioPlayer);
  211. % --- Executes on button press in btSynthStop.
  212. function btSynthStop_Callback(hObject, eventdata, handles)
  213. % hObject handle to btSynthStop (see GCBO)
  214. % eventdata reserved - to be defined in a future version of MATLAB
  215. % handles structure with handles and user data (see GUIDATA)
  216. global audioPlayer;
  217. if isempty(audioPlayer)
  218. return;
  219. end
  220. stop(audioPlayer);
  221. % --- Executes on button press in btClose.
  222. function btClose_Callback(hObject, eventdata, handles)
  223. % hObject handle to btClose (see GCBO)
  224. % eventdata reserved - to be defined in a future version of MATLAB
  225. % handles structure with handles and user data (see GUIDATA)
  226. close(handles.figPhSynth);
  227. % --- Executes during object creation, after setting all properties.
  228. function txtPlayDuration_CreateFcn(hObject, eventdata, handles)
  229. % hObject handle to txtPlayDuration (see GCBO)
  230. % eventdata reserved - to be defined in a future version of MATLAB
  231. % handles empty - handles not created until after all CreateFcns called
  232. % Hint: edit controls usually have a white background on Windows.
  233. % See ISPC and COMPUTER.
  234. if ispc
  235. set(hObject,'BackgroundColor','white');
  236. else
  237. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  238. end
  239. % --- If Enable == 'on', executes on mouse press in 5 pixel border.
  240. % --- Otherwise, executes on mouse press in 5 pixel border or over btPhSynth.
  241. function btPhSynth_ButtonDownFcn(hObject, eventdata, handles)
  242. % hObject handle to btPhSynth (see GCBO)
  243. % eventdata reserved - to be defined in a future version of MATLAB
  244. % handles structure with handles and user data (see GUIDATA)
  245. function playingStoped(varargin)
  246. global audioPlayer;
  247. buttonHandle = get(audioPlayer, 'UserData');
  248. set(buttonHandle, 'Enable', 'on');
  249. function playingStarted(varargin)
  250. global audioPlayer;
  251. buttonHandle = get(audioPlayer, 'UserData');
  252. set(buttonHandle, 'Enable', 'off');
  253. function updateAxes(phonemIndex, handles)
  254. global phonemes;
  255. global isVocalized;
  256. F0 = str2num(get(handles.txtF0Frequency, 'String'));
  257. ar_model = [1 phonemes(phonemIndex).AR];
  258. axes(handles.axesSpectr);
  259. cla;
  260. showSPM = get(handles.rbtSPM, 'Value');
  261. if showSPM == 1
  262. [h w] = freqz(1, ar_model);
  263. plot(w / (2 * pi), 10 * log10(abs(h) .^ 2)); %abs(h));%
  264. label = xlabel('????????????? ???????');
  265. set(label, 'FontName', 'MS Sans Serif');
  266. label = ylabel('???, ??');
  267. set(label, 'FontName', 'Arial Cyr');
  268. t = title('??????????? ????????? ???????? ??????');
  269. set(t, 'FontName', 'MS Sans Serif');
  270. xlim([0 0.5]);
  271. else
  272. plot(ar_model);
  273. t = title('???????????? ????????? ????????????');
  274. set(t, 'FontName', 'MS Sans Serif');
  275. xlim([1 length(ar_model)]);
  276. end
  277. axes(handles.axesTime);
  278. cla;
  279. y = synthPhonem(ar_model, F0, 8000, 500, isVocalized);
  280. plot(y);
  281. function txtF0Frequency_Callback(hObject, eventdata, handles)
  282. % hObject handle to txtF0Frequency (see GCBO)
  283. % eventdata reserved - to be defined in a future version of MATLAB
  284. % handles structure with handles and user data (see GUIDATA)
  285. index = get(handles.lstPhonemes, 'Value');
  286. updateAxes(index, handles);
  287. % --- Executes on button press in btCnahgeName.
  288. function btCnahgeName_Callback(hObject, eventdata, handles)
  289. % hObject handle to btCnahgeName (see GCBO)
  290. % eventdata reserved - to be defined in a future version of MATLAB
  291. % handles structure with handles and user data (see GUIDATA)
  292. global phonemes;
  293. global dbConnection;
  294. global groupID;
  295. global isGroupPhonems;
  296. index = get(handles.lstPhonemes, 'Value');
  297. phonemID = phonemes(index).ID;
  298. if isGroupPhonems == 1
  299. name = getGroupPhonemName(dbConnection, phonemID, groupID);
  300. else
  301. name = getPhonemName(dbConnection, phonemID);
  302. end
  303. if isequal(name.PHONEM_NAME{1}, 'null')
  304. oldName.Name = '';
  305. else
  306. oldName.Name = name.PHONEM_NAME{1};
  307. end
  308. newName = PhonemName(oldName);
  309. if ~isempty(newName)
  310. if isGroupPhonems == 1
  311. updateGroupPhonemName(dbConnection, phonemID, groupID, newName);
  312. else
  313. updatePhonemName(dbConnection, phonemID, newName);
  314. end
  315. names = get(handles.lstPhonemes, 'String');
  316. names{index} = sprintf('?????? /%s/', newName);
  317. set(handles.lstPhonemes, 'String', names);
  318. end
  319. % --- Executes on button press in btDataExport.
  320. function btDataExport_Callback(hObject, eventdata, handles)
  321. % hObject handle to btDataExport (see GCBO)
  322. % eventdata reserved - to be defined in a future version of MATLAB
  323. % handles structure with handles and user data (see GUIDATA)
  324. global dbConnection;
  325. global phonemes;
  326. [fileName filePath filterIndex] = uiputfile(...
  327. {'*.csv', '????????? ????? (*.csv)'; ...
  328. '*.*', '??? ????? (*.*)'}, ...
  329. '????????? ???');
  330. if ~(isequal(fileName, 0) | isequal(filePath, 0))
  331. fullName = strcat(filePath, fileName);
  332. if filterIndex == 1
  333. index = find(fileName == '.');
  334. if isempty(index)
  335. fullName = strcat(fullName, '.csv');
  336. end
  337. end
  338. for n = 1:length(phonemes)
  339. m(n,:) = phonemes(n).AR;
  340. end
  341. dlmwrite(fullName, m, ';');
  342. end
  343. % --- Executes on button press in btShowIR.
  344. function btShowIR_Callback(hObject, eventdata, handles)
  345. % hObject handle to btShowIR (see GCBO)
  346. % eventdata reserved - to be defined in a future version of MATLAB
  347. % handles structure with handles and user data (see GUIDATA)
  348. global phonemes;
  349. phonemIndex = get(handles.lstPhonemes, 'Value');
  350. phonemModel = phonemes(phonemIndex).AR;
  351. InterPhonemIR(phonemModel);
  352. % --- Executes on button press in rbtSPM.
  353. function rbtSPM_Callback(hObject, eventdata, handles)
  354. % hObject handle to rbtSPM (see GCBO)
  355. % eventdata reserved - to be defined in a future version of MATLAB
  356. % handles structure with handles and user data (see GUIDATA)
  357. set(handles.rbtSPM, 'Value', 1);
  358. set(handles.rbtLPC, 'Value', 0);
  359. index = get(handles.lstPhonemes, 'Value');
  360. updateAxes(index, handles);
  361. % --- Executes on button press in rbtLPC.
  362. function rbtLPC_Callback(hObject, eventdata, handles)
  363. % hObject handle to rbtLPC (see GCBO)
  364. % eventdata reserved - to be defined in a future version of MATLAB
  365. % handles structure with handles and user data (see GUIDATA)
  366. set(handles.rbtSPM, 'Value', 0);
  367. set(handles.rbtLPC, 'Value', 1);
  368. index = get(handles.lstPhonemes, 'Value');
  369. updateAxes(index, handles);
  370. % --- Executes on button press in btClear.
  371. function btClear_Callback(hObject, eventdata, handles)
  372. % hObject handle to btClear (see GCBO)
  373. % eventdata reserved - to be defined in a future version of MATLAB
  374. % handles structure with handles and user data (see GUIDATA)
  375. % speakerID = varargin{2};
  376. global phonemes;
  377. global dbConnection;
  378. global speakerID;
  379. global groupID;
  380. global isGroupPhonems;
  381. N = size(phonemes);
  382. order = size(phonemes(1).AR);
  383. AR_noise = [1];
  384. for m=1:1:order(1,2)
  385. AR_noise = [AR_noise;0];
  386. end;
  387. min_IE = 100000;
  388. index = 0;
  389. ID = 0;
  390. name = getPhonemNameBySpeaker(dbConnection, speakerID);
  391. for k = 1:1:N(1,2)
  392. IE = compareSPM(AR_noise,[1,phonemes(k).AR]'); % compareSPM([1,phoneme(k).AR]',AR_noise); %
  393. if IE < min_IE & isequal(name.PHONEM_NAME{k}, 'null')
  394. ID = phonemes(k).ID;
  395. index = k;
  396. min_IE = IE;
  397. end;
  398. end;
  399. s = sprintf('?? = %0.5g',min_IE);
  400. set(handles.IEtext,'String',s);
  401. if isGroupPhonems == 1
  402. updateGroupPhonemName(dbConnection, ID, groupID, '#');
  403. else
  404. updatePhonemName(dbConnection, ID, '#');
  405. end;
  406. names = get(handles.lstPhonemes, 'String');
  407. names{index} = sprintf('?????? /#/');
  408. set(handles.lstPhonemes, 'String', names);
  409. set(handles.lstPhonemes, 'Value', index);
  410. updateAxes(index, handles);
  411. % --- Executes on button press in btDelPhoneme.
  412. function btDelPhoneme_Callback(hObject, eventdata, handles)
  413. % hObject handle to btDelPhoneme (see GCBO)
  414. % eventdata reserved - to be defined in a future version of MATLAB
  415. % handles structure with handles and user data (see GUIDATA)
  416. global phonemes;
  417. global dbConnection;
  418. global groupID;
  419. global isGroupPhonems;
  420. index = get(handles.lstPhonemes, 'Value');
  421. % ???????? ??? ?? ??
  422. phonemID = phonemes(index).ID;
  423. if isGroupPhonems == 1
  424. deleteGroupPhonem(dbConnection, groupID);
  425. else
  426. sqlstatement = sprintf('DELETE * FROM PHONEMES WHERE PHNM_ID=%d', phonemID);
  427. exec(dbConnection, sqlstatement);
  428. end;
  429. % ???????? ??? ?? ?????? ?????
  430. names = get(handles.lstPhonemes, 'String');
  431. N = size(names);
  432. new_names(1:index-1) = names(1:index-1);
  433. new_names(index:N(1,1)-1) = names(index+1:N(1,1));
  434. set(handles.lstPhonemes, 'String', new_names);
  435. % ???????? ??? ?? ??????? phonemes
  436. phonemes(index:N(1,1)-1) = phonemes(index+1:N(1,1));
  437. phonemes(N(1,1)) = [];
  438. set(handles.IEtext,'String','');
  439. if index == 1
  440. updateAxes(index+1, handles);
  441. set(handles.lstPhonemes, 'Value', index+1);
  442. else
  443. updateAxes(index-1, handles);
  444. set(handles.lstPhonemes, 'Value', index-1);
  445. end;