Dawn@4: function [F1, F2, F3, F4, B1, B2, B3, B4, err] = func_SnackFormants(wavfile, windowlength, frameshift, preemphasis) Dawn@4: % [F1, F2, F3, F4, B1, B2, B3, B4, err] = func_SnackFormants(wavfile, windowlength, frameshift, preemphasis) Dawn@4: % Input: wavfile - input wav file Dawn@4: % windowlength - windowlength in seconds Dawn@4: % frameshift - in seconds Dawn@4: % preemphasis - wonder what this could be? Dawn@4: % Output: Fx, Bx - formant and bandwidth vectors Dawn@4: % err - error flag Dawn@4: % Notes: If on Windows/PC, this function will call up the Snack executable Dawn@4: % in the Windows folder. Otherwise, it will try to call up Snack - this Dawn@4: % require Snack to be installed beforehand. Dawn@4: % Dawn@4: % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA Dawn@4: % Copyright UCLA SPAPL 2009 Dawn@4: Dawn@4: % settings Dawn@4: iwantfilecleanup = 1; %delete tcl,f0,frm files when done Dawn@4: tclfilename = 'tclforsnackformant.tcl'; %produce tcl file to call snack Dawn@4: Dawn@4: % make the wavfile acceptable to tcl in Snack Dawn@4: formantfile = [wavfile(1:end-3) 'frm']; Dawn@4: wavfile = strrep(wavfile, '[', '\['); Dawn@4: wavfile = strrep(wavfile, ']', '\]'); Dawn@4: wavfile = ['"' wavfile '"']; Dawn@4: %wavfile = strrep(wavfile, '\', '\\'); % for PC Dawn@4: Dawn@4: if (ispc) % pc can run snack.exe Dawn@4: wavfile = strrep(wavfile, '\', '\\'); Dawn@4: cmds = sprintf('-windowlength %.3f -framelength %.3f -windowtype Hamming -lpctype 1 -preemphasisfactor %.3f -ds_freq 10000', windowlength, frameshift, preemphasis); Dawn@4: err = system(['Windows\snack.exe formant ' wavfile ' ' cmds]); Dawn@4: Dawn@4: else % for macs and possibly others Dawn@4: %cmd = 'tclsh'; % this is for older macs Dawn@4: cmd = 'wish8.4'; % seems to work for OSX Snow Leopard Dawn@4: %cmd = 'wish84'; % for PC Dawn@4: Dawn@4: fid = fopen(tclfilename, 'wt'); Dawn@4: fprintf(fid, '#!/bin/sh\n'); Dawn@4: fprintf(fid, '# the next line restarts with wish \\\n'); Dawn@4: fprintf(fid, 'exec wish8.4 "$0" "$@"\n\n'); Dawn@4: fprintf(fid, 'package require snack\n\n'); Dawn@4: fprintf(fid, 'snack::sound s\n\n'); Dawn@4: fprintf(fid, 's read %s\n\n', wavfile); Dawn@4: fprintf(fid, 'set fd [open [file rootname %s].frm w]\n', wavfile); Dawn@4: fprintf(fid, 'puts $fd [join [s formant -windowlength %.3f -framelength %.3f -windowtype 1 -lpctype 1 -preemphasisfactor %.3f -ds_freq 10000] \\n]\n', windowlength, frameshift, preemphasis); Dawn@4: fprintf(fid, 'close $fd\n\n'); Dawn@4: fprintf(fid, 'exit'); Dawn@4: fclose(fid); Dawn@4: Dawn@4: err = system([cmd ' ' tclfilename]); Dawn@4: end Dawn@4: Dawn@4: % change back into original file Dawn@4: wavfile = strrep(wavfile, '\[', '['); Dawn@4: wavfile = strrep(wavfile, '\]', ']'); Dawn@4: Dawn@4: % oops, error, exit now Dawn@4: if (err~=0) Dawn@4: F1 = NaN; F2 = NaN; F3 = NaN; F4 = NaN; Dawn@4: B1 = NaN; B2 = NaN; B3 = NaN; B4 = NaN; Dawn@4: if (iwantfilecleanup) Dawn@4: formantfile = strrep(formantfile, '\\', '\'); Dawn@4: if (exist(formantfile, 'file') ~= 0) Dawn@4: delete(formantfile); Dawn@4: end Dawn@4: Dawn@4: if (exist(tclfilename, 'file') ~= 0) Dawn@4: delete(tclfilename); Dawn@4: end Dawn@4: end Dawn@4: return; Dawn@4: end Dawn@4: Dawn@4: % snack call was successful, read out formant values Dawn@4: [F1,F2,F3,F4,B1,B2,B3,B4] = textread(formantfile, '%f %f %f %f %f %f %f %f'); Dawn@4: Dawn@4: if (iwantfilecleanup) Dawn@4: formantfile = strrep(formantfile, '\\', '\'); Dawn@4: if (exist(formantfile, 'file') ~= 0) Dawn@4: delete(formantfile); Dawn@4: end Dawn@4: Dawn@4: if (exist(tclfilename, 'file') ~= 0) Dawn@4: delete(tclfilename); Dawn@4: end Dawn@4: end