Praat script to export all possible data about audio - praat

Maybe someone has the Praat script, which can get all possible information about the Audio file (pitches, formats, intensity)?.

Assuming that by "all possible data about audio" you only mean fundamental frequency, formant structure and intensity contour (and not, say, spectra, pulses, etc), the easiest way to do this is to generate respectively a Pitch, Formant, and Intensity objects.
pitch = To Pitch: 0, min_f0, max_f0
formant = To Formant (burg): 0,
... total_formants, max_formant, 0.025, 50
intensity = To Intensity: min_f0, 0, "yes"
You'll still need to know some things about the audio you're processing, though, like the likely frequency of the maximum formant you are interested in, or the range within which you estimate the fundamental to be (you might want to look at this plugin with automatic methods for estimating f0 range).
As for the exporting, what I assume you mean by this is that you want this information to be accessible from a program that is not Praat. This might be the hardest part, since Praat does not have any standard way to export data, and the data formats that it uses, although they are all text-based, are all very ... Praat-specific (you can check them out by using the Save as text file... command).
You could process them within Praat and put the data you want into a Table object with whatever format and structure you want and save it as either a tab or a comma separated file (see my related answer). To get started, you could use the Down to Table... command available for Formant objects, which will create a Table with the formant data. You can then expand that Table to include data from the Pitch and Intensity objects (or whatever objects you need).
Alternatively, most (=not all) text-based formats used by Praat are almost YAML, so you could try to convert them and read them as-is into whatever program you want to use later on. I wrote a couple of Perl scripts that do just this, converting to and from JSON/YAML, and a Praat plugin to do this from the Praat GUI. You might want to check those out.

Here's a script solves the problem.
form Give the parameters for pause analysis
comment soundname:
text soundname 1.wave
comment outputFileName.csv:
text outputFileName result.csv
endform
min_f0 = 75
max_f0 = 350
Read from file: soundname$
soundname$ = selected$ ("Sound")
select Sound 'soundname$'
formant = To Formant (burg): 0, 4, 5000, 0.025, 50
formantStep = Get time step
selectObject: formant
table = Down to Table: "no", "yes", 6, "yes", 3, "yes", 3, "yes"
numberOfRows = Get number of rows
select Sound 'soundname$'
pitch = To Pitch: 0, min_f0, max_f0
selectObject: table
Append column: "Pitch"
for step to numberOfRows
selectObject: table
t = Get value: step, "time(s)"
selectObject: pitch
pitchValue = Get value at time: t, "Hertz", "Nearest"
selectObject: table
Set numeric value: step, "Pitch", pitchValue
endfor
#export to csv
selectObject: table
Save as comma-separated file: outputFileName$
removeObject(table)
echo Ok

Related

Create PySimpleGUI list with a key

I wish to have a list of text items in a PySimpleGUI that I can update later. That is, I want to have a key for the list. This might be vertical or horizontal, and I do not know how many items there will be.
I end up with different use cases, but the current one is to make a single line of text items with different colors. Other times, I need to write and update a customized table, just different enough that the table widget does not work.
Conceptually, I want to do something like this:
layout = [ [sg.T('Titles and Things')], sg.ListThing(key='-data-', [[]]) ]
so that I can:
window['-data-'].update(values=[ [sg.T(v, color=c)] for (v,c) in my_data ])
Another, invalid syntax, way of saying what I want is to use [key="-data-", sg.T('Item1'), sg.T('Item2')].
Is this possible?
You can update individual layout elements but you cannot dynamically change the layout itself.
It is possible to create 2 or more elements, whereby only one of them is visible, and switch them later as needed. Or you can close and re-create the window with another layout. Or combine both approaches.
An example of switching layouts:
def change_layout():
left_col_1 = sg.Column([[sg.Text(f'Text {i}') for i in range(4)]], visible=True, key='col_1')
left_col_2 = sg.Column([[sg.Text(f'Text {i}')] for i in range(6)], visible=False, key='col_2')
visible_1 = True
layout = [[sg.Column([[left_col_1, left_col_2]]), sg.Button('Change layout', key='change')]]
window = sg.Window('window', layout=layout, finalize=True)
while True:
event, values = window.read()
print(event)
print(values)
print(visible_1)
if event in ('Exit', sg.WIN_CLOSED):
break
if event == 'change':
window['col_1'].update(visible=not visible_1)
window['col_2'].update(visible=visible_1)
visible_1 = not visible_1
Please notice that the alternative layouts for the left part (left_col_1, left_col_2) need to be enclosed in a container (column, frame) to keep their position in the window in the moment they are invisible.

Divide an image into non-overlapping blocks and applying the 2D DWT on each block

I am working on creating an image splicing detection software so I need to divide the image into non-overlapping blocsk and apply Discrete Meyer Wavelet Transform on each block of the image
I have tried the blockproc function to do that but I got no result:
I = imread('pears.png');
fun = #(block_struct)...
dwt2(block_struct.data,'dmey');
C = blockproc(I,[64 64],fun);
So how can I access the [cA,cH,cV,cD] of dwt2 using the above code?
blockproc assumes that you are outputting an actual image. You cannot use this for multiple outputs. If you truly want this to work with blockproc, you will unfortunately need to call blockproc four times, with each time extracting the different set of coefficients for the directions. Also note that the 2D DWT only works for grayscale images, so you need to convert to grayscale before actually doing any processing. The pears image you've chosen is a colour / RGB image.
I'd like to reference this post on how to select the Nth output given an input function: How do I get the second return value from a function without using temporary variables?. You will need to save this code to a file called nth_output.m, which allows you to programatically extract all output variables from a function and choose only one output.
function value = nth_output(N,fcn,varargin)
[value{1:N}] = fcn(varargin{:});
value = value{N};
end
Simply omitting the extra output arguments when you call the function only gives you the first output, which is what your blockproc code is doing. Once you do that, it's a matter of creating 4 anonymous functions to capture each output from dwt2, and running blockproc 4 times. Make sure you specify which output you want for each of the anonymous functions, so 1 up to 4 and you simply provide a handle to the function you want to run in addition to the input arguments that go into the function.
Therefore, try something like this:
I = rgb2gray(imread('pears.png'));
fun1 = #(block_struct) nth_output(1, #dwt2, block_struct.data,'dmey');
fun2 = #(block_struct) nth_output(2, #dwt2, block_struct.data,'dmey');
fun3 = #(block_struct) nth_output(3, #dwt2, block_struct.data,'dmey');
fun4 = #(block_struct) nth_output(4, #dwt2, block_struct.data,'dmey');
I = rgb2gray(I);
cA = blockproc(I,[64 64],fun1);
cH = blockproc(I,[64 64],fun2);
cV = blockproc(I,[64 64],fun3);
cD = blockproc(I,[64 64],fun4);
cA, cH, cV, and cD contain the DWT coefficients you need for each set of directions.

How to move an image in Lua?

I am new to Lua programming and I am having problems while trying to move an image from a set of coordinates to another.
What I am trying to create is to be used with the X-Plane flight simulator. There is a library called SASL (www.1-sim.com), that was created to make plugins (for X-Plane) creation easir, since the default language is C++ and many people find it difficult.
In general, SASL works as a bridge between Lua and X-Plane, in general lines, the scripts you write reads some data straight from X-Plane (DataRefs) while it is running and depending on the code you wrote its execute commands or many other things that it is possible.
So, when using SASL to create cockpit/panel gauges it uses a base file, named 'avionics.lua' that works as a class and loads all gauges you create for the specific aircraft you are working on. For example my avionics.lua file looks like this:
size = { 2048, 2048 }
components = {
flaps {};
};
where, 'size' is the size that will be used for things to be drawn and components is an array of gauges, in this case the flaps gauge.
The rest of the work is to create the gauge functionality and this is done in a separate file, in my case, called 'flaps.lua'.
Within flaps.lua, is where I need to code the flaps indicator functionality which is to load 2 images: one for the back ground and the second one for the flaps indicator.
The first image is a fixed image. The second one will move throught the 'y' axis based on the flaps indicator DataRef (flapsDegree property below).
The code below when X-Plane is running displays the background image and the flaps indicator on its first stage which is 0 as you can see on the image.
size = {78,100}
local flapsDegree = globalPropertyf("sim/cockpit2/controls/flap_ratio")
local background = loadImage("gfx/Flaps.png")
local indicator = loadImage("gfx/Flaps_Indicator.png")
local flaps = get(flapsPosition)
components = {
texture { position = {945, 1011, 60, 100}, image = background},
texture { position = {959, 1097, 30, 9}, image = indicator},
}
Image
Now, the problem comes when I need to implement the logic for moving the 'indicator' image through the 'y' axis.
I have tried this code without success:
if flaps == 0.333 then
indicator.position = {959, 1075, 30, 9}
end
So how could I accomplish that?
I am not familiar with the SALS library. I just had a quick look into it.
Everything you need to know is in the manuals on the website you linked.
In particular http://www.1-sim.com/files/SASL300.pdf
Everytime your screen is updated each components draw() function will be called.
So if you want to change something dynamically you have to put that into the component's draw function.
If you open the SALS sources you'll find basic components which show you how to use that stuff. One of them is needle.lua:
-- default angle
defineProperty("angle", 0)
-- no image
defineProperty("image")
function draw(self)
local w, h = getTextureSize(get(image))
local max = w
if h > max then
max = h
end
local rw = (w / max) * 100
local rh = (h / max) * 100
drawRotatedTexture(get(image), get(angle),
(100 - rw) / 2, (100 - rh) / 2, rw, rh)
end
If you check the manual you'll find that there is not only a drawRotatedTexture function but many other functions for drawing stuff. Just play around. Try drawTexture for your purpose.
If you don't know what you have to program, open every single lua file in the library and read it together with the Lua reference manual and the SALS documentation until you understand what is going on. Once you understand the existing code you can extend it with ease.

Making a gif from images

I have a load of data in 100 .sdf files (labelled 0000.sdf to 0099.sdf), each of which contain a still image, and I'm trying to produce a .gif from these images.
The code I use to plot the figure are (in the same directory as the sdf files):
q = GetDataSDF('0000.sdf');
imagesc(q.data');
I've attempted to write a for loop that would plot the figure and then save it with the same filename as the sdf file but to no avail, using:
for a = 1:100
q=GetDataSDF('0000.sdf');
fh = imagesc(q.dist_fn.x_px.Left.data');
frm = getframe( fh );
% save as png image
saveas(fh, 'current_frame_%02d.jpg');
end
EDIT: I received the following errors when trying to run this code:
Error using hg.image/get
The name 'Units' is not an accessible property for an instance of class 'image'.
Error in getframe>Local_getRectanglesOfInterest (line 138)
if ~strcmpi(get(h, 'Units'), 'Pixels')
Error in getframe (line 56)
[offsetRect, absoluteRect, figPos, figOuterPos] = ...
Error in loop_code (line 4)
frm = getframe( fh );
How do I save these files using a for loop, and how do I then use those files to produce a movie?
The reason for the error is that you pass an image handle to getframe, but this function excpects a figure handle.
Another problem is that you always load the same file, and that you saveas will not work for gifs. (For saving figures as static images, maybe print is the better option?)
I tried to modify my own gif-writing loop so that it works with your data. I'll try to be extra explicit in the comments, since you seem to be starting out. Remember, you can always use help name_of_command to display a short Matlab help.
% Define a variable that holds the frames per second your "movie" should have
gif_fps = 24;
% Define string variable that holds the filename of your movie
video_filename = 'video.gif';
% Create figure 1, store the handle in a variable, you'll need it later
fh = figure(1);
for a = 0:99
% Prepare file name so that you loop over the data
q = GetDataSDF(['00' num2str(a,'%02d') 'sdf']);
% Plot image
imagesc(q.dist_fn.x_px.Left.data');
% Force Matlab to actually do the plot (it sometimes gets lazy in loops)
drawnow;
% Take a "screenshot" of the figure fh
frame = getframe(fh);
% Turn screenshot into image
im = frame2im(frame);
% Turn image into indexed image (the gif format needs this)
[imind,cm] = rgb2ind(im,256);
% If first loop iteration: Create the file, else append to it
if a == 0;
imwrite(imind,cm,video_filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,video_filename,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
end
One more note: When the size of the data is the same for each plot, it makes sense to only use the plot(or in this case, imagesc) command once, and in later loop iterations replace it with a set(ah,'Ydata',new_y_data) (or in this case set(ah,'CData',q.dist_fn.x_px.Left.data'), where ah is a handle of the plot axes (not the plot figure!). This is orders of magnitude faster than creating a whole new plot in each loop iteration. The downside is that the scaling (here, the color-scaling) will be the same for each plot. But in every case that I have worked on so far, that was actually desirable.

how to apply AccountingForm to TableView without affecting the row/column labels?

I am using M 8.0.4.
I am use TableView in demo Manipulate[] to display final solution data, which is a matrix of numerical data.
TableView is very nice to use, since it comes with an automatic scroll bars to move down the rows and to the right over the columns, so that one can see the whole matrix, while keeping the overall display area fixed. (I have an image at the bottom)
The problem that I am not able to find a solution for, is that I need to format the matrix data, so that it looks nice. Otherwise, if the a data element is too large to fit in a cell, it will wrap around, making the TableView misaligned. I also need to adjust decimal point an such other formatting.
I can't apply NumberForm nor AccountForm to the data, and then apply TableView on the result, because TableView does not like to see those wrappers, it needs the numbers.
But if apply the AccountingForm to the TableView, then the numbers that represent the row numbers and column numbers, which are added by TableView automatically, get formatted as well. And I do not want those formatted as well. A row number should remain an integer, not floating point. I just want the data itself formatted.
I could not figure how to format the data from inside Table view. When I use FullForm to see how the data is kept inside TableView, I could not figure how to format it without breaking TableView.
I'll show the problem, then show what I tried.
(* make up some data, and make a TableView of it *)
data = {{-1234.8, 0.123}, {0.12345678, 0.1234}}
tbl = TableView[data, ItemSize -> {{3, {8}}}, DefaultBaseStyle -> 11,
Spacings -> {.2, .1}, FrameStyle -> Gray]
Notice the row numbers and columns number (marked) are positive integers.
Now, I wanted to format the data itself, (the exact formatting options used below is just an example)
AccountingForm[tbl, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True]
But now the Table rows and columns are also formatted:
The FullForm of TableView is:
In[156]:= FullForm[tbl]
TableView[List[List[-1234.8`,0.123`],List[0.12345678`,0.1234`]],
Rule[ItemSize,List[List[3,List[8]]]],
Rule[DefaultBaseStyle,11],Rule[Spacings,List[0.2`,0.1`]],
Rule[FrameStyle,GrayLevel[0.5`]]]
So the data in TableView can be pulled out using
In[166]:= tbl[[1]]
Out[166]= {{-1234.8,0.123},{0.12345678,0.1234}}
But when I change tbl[[1]], using ReplacePart[], with an AccountingForm version of the data, TableView no longer works:
formattedData =
AccountingForm[data, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", "0"}, SignPadding -> True];
In[245]:= tbl=ReplacePart[tbl,1->formatted]
Out[245]= TableView[{{-1234.8,+0.123},{+0.123,+0.123}},
ItemSize->{{3,{8}}},DefaultBaseStyle->11,Spacings->{0.2,0.1},
FrameStyle->GrayLevel[0.5]]
So, I broke TableView. Since it does not display any more.
Question is: How to format numeric data that goes into TableView without affecting the row/column index values?
Fyi, this is how the TableView looks inside one Manipulate demo I have. Notice the automatics scroll-bars. (In this one, I am not using NumberForm and its friends to do the formatting. But what I did is not very efficient, I'd rather use NumberForm if I can, hence my question)
thanks
update 12/22/11 1:30 AM
Here is a complete code example for Mike to follow up on his answer below
data = {{-1234.8, 0.123}, {0.12345678, 0.1234}};
newData =
Map[AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True] &, data, {2}];
tbl = TableView[newData, ItemSize -> {{3, {8}}}, Spacings -> {.2, .1},
FrameStyle -> Gray]
Now how exactly do I use the Cell command for the above? (This is in Manipulate, not a notebook session, Manipulate runs all in one cell. I can't make separate cells and such in this code. But for trying it, I can in a new notebook, but the actual code that will use this solution, has to run in a Manipulate with no Cells.
Update 12/22/11 5 am
I am noticing some not good performance of TableView. Consider this code
Remove["Global`*"];
ClearSystemCache[]
m1 = MemoryInUse[$FrontEnd];
N[m1/10^6]
n = 256;
data = Table[RandomReal[], {n}, {n}];
TableView[data, ContentSize -> {300, 300}]
m2 = MemoryInUse[$FrontEnd] - m1;
N[m2/10^6]
The data itself, assuming double for the reals, is about half MB only. ByteCount says
2 MB due to other bookkeeping data struct.
In[114]:= ByteCount[data]/10^6//N
Out[114]= 2.10948
But Front end seems to use much more RAM (for the whole TableView I mean not just the data), sometimes I get 20 MB and sometimes much more (got 100 MB at one time). But if you try the above on your computer, you'll notice M is having hard time with this. I think it might be the rendering of the table part that causes M to take so much time.
I do not think 256 by 256 is such a large matrix. Even with 128 by 128, it was having hard time to render it on the screen. Once it is up, then it is fast and no problem using it.
It looks like TableView is not yet optimized well. I think I will only use it to display small part of the solution Matrix as the performance is not good to use it in a demo to display the whole solution, it will make the demo look bad.
The problem is that my solution matrix can be large, and I wanted a away to display the data in limited amount of GUI space. TableView is all what I can find that comes with scrollbars build-in. Nothing else in the Manipulate controls has something like this to use, so I have to use TableView.
Update 12/22/11 4 PM
Thanks for the hint by Mike below, I am now looking at using Pane with Scollbars, and it is working better than TableView. (still little slow with large data, but not as bad as TableView)
Here is a code example of doing that:
n = 32;
data = Table[RandomReal[], {n}, {n}];
grid = Grid[data, Spacings -> {.4, .4}, Alignment -> Left, Frame -> All];
grid = Style[NumberForm[grid, {6, 5}], LineBreakWithin -> False];
pane = Pane[grid, ImageSize -> {200}, Scrollbars -> True]
Only problem I noticed so far is that when n gets large, Pane generates this outer frame saying very large output was generated. But I am sure there is a programmatic way (option somewhere) to turn that effect off, I just started looking at this and should be able to find a way around this once I have my coffee. (fingers crossed)
good news
The "Very large output was generated" ONLY showed up when in notebook interface. When I put the code in a Manipulate and and run it, it did not show this output frame. So, I am happy now and this issue is closed. I'll use Pane from now on. (Thanks again to Mike)
btw, to remove the message in question, here is a useful link I found:
http://forums.wolfram.com/mathgroup/archive/2009/Apr/msg00935.html
but I did not need to do anything for what I am doing.
I guess this is a different enough method to post as another answer, though it clearly borrows from Mike's answer.
f = ToString #
AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True] &;
data = {{-1234.8, 0.123}, {0.12345678, 0.1234}};
TableView[Map[f, data, {2}], ItemSize -> {{3, {8}}},
DefaultBaseStyle -> 11, Spacings -> {.2, .1}, FrameStyle -> Gray]
Since using ToString was non-intuitive, I decided to explore it a bit more. It seems that TableView suppresses quotation marks in strings, but only bare strings. e.g.:
string = "\"\"\"test\"";
TableView[{{ string }}]
TableView[{{ {string} }}]
Other interesting behavior can be seen with 2D formatted strings:
string = "\!\(\*SuperscriptBox[SqrtBox[\"5\"], \"2\"]\)";
TableView[{{ string }}]
TableView[{{ {string} }}]
One suggestion:
Firstly make each element in your data into AccountingForm, or whatever Form you want:
f = AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True] &
newdata = {f[#[[1]]], f[#[[2]]]} & /# data
Note that I've had a bit of a brain fade doing this. I'm sure there is an easier way than Mapping (Edit: the brain fade made me forget levelspec). This will give you output like this:
The problem of course is that you now have string characters showing and "conventional" attempts to switch this off failed ("conventional" == options inspector, Style[] etc.). But you can fix this by modifying two styles that exist in the Core.nb stylesheet. So you will need to edit the private styles in your current notebook -- and also remove DefaultBaseStyle from your code because it will cause conflicts with the stylesheet style. So Format > Edit Stylesheet and add this style:
Cell[StyleData["TableViewItemExpression"],
ShowStringCharacters->False,
TemplateBoxOptions->{DisplayFunction->(StyleBox[#, Deployed -> False]& )}]
Actually that is really all you need to do but if you subsequently wanted to do some additional styling of the numbers then you need to modify a different style. Here is an example making the numbers red:
Cell[StyleData["TableViewGrid"],
ContextMenu->FEPrivate`FrontEndResource["ContextMenus", "TableView"],
ShowAutoStyles->False,
AutoSpacing->False,
ShowStringCharacters->False,
AutoNumberFormatting->True,
LineIndent->0,
FontWeight->"Plain",
FontColor->RGBColor[1, 0, 0]]
And this is what it looks like:

Resources