Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# matlab autosave files
*.asv
*.m~
# other files being used in testing
audioGUI.m
*.mat
*/*
temp_wav.wav
47 changes: 47 additions & 0 deletions get_fast_tracks.praat
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# get_fast_tracks.praat by Henry Nomeland, 21 Mar 2024
# adapted from get_formants.praat by Ben Parrell, 18 Dec 2017
#
# measures formants for a .wav file utilizing tools and utils of FastTrack
# FastTrack code available here: https://github.com/santiagobarreda/FastTrack/
# for use with wave_viewer

#Inputs
form Measure formant values for segments in a textgrid
sentence dir: /Users/Ben/Documents/MATLAB/wave_viewer
sentence file_name: temp_wav
endform

include utils/trackAutoselectProcedure.praat
@getSettings

formants = 3
time_step = 0.002
steps = 30
coefficients = 3
out_formant = 2
lowestAnalysisFrequency = 4800
highestAnalysisFrequency = 5500

method$ = "burg"
wav_name$ = file_name$ + ".wav"
Read from file... 'dir$'/'wav_name$'
soundID1$ = selected$("Sound")
select Sound 'soundID1$'

@trackAutoselect: selected(), dir$, lowestAnalysisFrequency, highestAnalysisFrequency, steps, coefficients, formants, method$, 0, selected(), 0, 4000, 2, 0, 0

formant = selected ("Formant")

#write formants
formant_name$ = file_name$ + "_formants.txt"
Down to Table... yes yes 6 no 3 yes 3 no
Save as tab-separated file... 'dir$'/'formant_name$'

#extract LPC parameters
#selectObject: formant
#To LPC... fs
#Down to Matrix (lpc)

#save LPC parameters
#lpc_name$ = file_name$ + "_lpc.txt"
#Save as headerless spreadsheet file... 'dir$'/'lpc_name$'
68 changes: 68 additions & 0 deletions get_formant_tracks.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
case 'mine', ftrack_func = @my_ftrack_func; params.nformants_max = 3; params.yes_trackperframe = 1;
case 'mine2', ftrack_func = @my_ftrack_func2; params.nformants_max = 4; params.yes_trackperframe = 1;
case 'praat', ftrack_func = @praat_ftrack_func; params.nformants_max = 5; params.yes_trackperframe = 0;
case 'fasttrack', ftrack_func = @fasttrack_ftrack_func; params.nformants_max = 5; params.yes_trackperframe = 0;
otherwise, error('formant tracking method(%s) unrecognized',ftrack_method);
end
if ~nformants, nformants = params.nformants_max; end
Expand Down Expand Up @@ -201,6 +202,73 @@
output{2} = lpc_coeffs;
output{3} = msaxis * 1000; %covert from s to ms

%%% function which executes Fast Track script using params
function output = fasttrack_ftrack_func(y,params)
%must be in git repo folder to run praat function, so save current location and go there
curr_dir = pwd;
temp_str = which('get_fast_tracks.praat');
praat_path = fileparts(temp_str);
cd(praat_path)

fs = params.fs;
nformants = params.nformants;

%%% Praat wrapper code here
% write y to file (this will be deleted later, it is just written to the
% current directory)
audiowrite('temp_wav.wav',y,fs)

if ismac
status = system(['"/Applications/Praat.app/Contents/MacOS/Praat" --run get_fast_tracks.praat "' pwd '" "temp_wav"']);
else
status = system(['"C:\Users\Public\Desktop\Praat.exe" --run get_fast_tracks.praat "' pwd '" "temp_wav"']);
end
if status ~= 0
error('Something went wrong in Praat analysis')
end

% clean up praat output text file to eliminate uninterpretable characters
A = regexp( fileread('temp_wav_formants.txt'), '\n', 'split');
headers = strsplit(A{1},'\t');
if length(headers) == 1 % for some reason Praat sometimes uses a space to separate headers
headers = strsplit(A{1},' ');
end
for i = 1:length(headers)
curTxt = headers{i};
startUnits = strfind(curTxt,'(');
if ~isempty(startUnits)
curTxt = curTxt(1:startUnits-1);
headers{i} = curTxt;
end
end
A{1} = strjoin(headers,'\t');
fid = fopen('temp_wav_formants.txt','w');
fprintf(fid, '%s\n', A{:});
fclose(fid);

% load formant tracks from file written by Praat and put in 'formant' output
formant_vals = readtable('temp_wav_formants.txt','Delimiter','\t');

%find number of formant values returned by praat
for nf = 1:nformants
form=['F' num2str(nf)]; % finds the particular formant number
formant(nf,:) = formant_vals.(form)'; % updates row in formant
end

%find times of formants
msaxis = formant_vals.time';

% clean up by deleting files and return to previous directory
delete temp_wav.wav
delete temp_wav_formants.txt
cd(curr_dir)

% establish outputs
lpc_coeffs = [];
output{1} = formant;
output{2} = lpc_coeffs;
output{3} = msaxis * 1000; %covert from s to ms
%%% end of Fast Track function

% my version of the colea frmnts function,
% orinally copyright (c) 1998 by Philipos C. Loizou
Expand Down
8 changes: 4 additions & 4 deletions get_formant_tracks.praat
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#Inputs
form Measure formant values for segments in a textgrid
sentence directory_name: /Users/Ben/Documents/MATLAB/wave_viewer
sentence file_name: temp_wav
sentence file_name: temp_wav
positive maximum_formant 5500
positive number_of_formants 5
positive window_size 0.025
positive window_size 0.025
positive time_step 0.005
positive preemphasis 50
positive fs 11025
positive preemphasis 50
positive fs 11025
endform

#open the sound and select it
Expand Down
8 changes: 2 additions & 6 deletions wave_viewer.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
%% set up GUI

% create new figure
hf = figure('Name',p.plot_params.name,'Units','normalized','Position',p.plot_params.figpos);
hf = figure('Name',p.plot_params.name,'Units','normalized','Position',p.plot_params.figpos);
set(hf,'DeleteFcn',@delete_func);

p.guidata = guihandles(hf);
Expand Down Expand Up @@ -390,10 +390,6 @@ function edit_events(hObject,eventdata) % callback for h_button_edit_events
end
end





% clear events button
clearEventsButtonPos = [padL+buttonWidth*1/2 horiz_orig buttonWidth*1/2 buttonHeight];
hbutton.clear_events = uicontrol(p.guidata.buttonPanel,'Style','pushbutton',...
Expand Down Expand Up @@ -669,6 +665,7 @@ function toggle_formants(hObject,eventdata) % callback for hbutton.toggle_forman
end
set(gram_ax,'UserData',axinfo);
end

% calc button
calcButtonPos = [padL horiz_orig buttonWidth buttonHeight];
hbutton.calc = uicontrol(p.guidata.buttonPanel,'Style','pushbutton',...
Expand Down Expand Up @@ -2312,7 +2309,6 @@ function set_viewer_axlims(hax,t_min,t_max,dat4minmax,yax_fact2use)
'ptrack_octave_cost', 0.01, ...
'ptrack_octave_jump_cost', 0.35, ...
'ptrack_voiced_unvoiced_cost', 0.14);

end

%% get figure params (used to be global vars)
Expand Down