{"id":17247633,"url":"https://github.com/joshdata/praat-py","last_synced_at":"2025-04-12T04:30:54.384Z","repository":{"id":5904131,"uuid":"7122942","full_name":"JoshData/praat-py","owner":"JoshData","description":"From my PhD days: Praat-Py is a custom build of Praat, the computer program used by linguists for doing phonetic analysis on sound files, to allow for scripts to be written in the Python programming language, rather than in Praat's built-in language.","archived":false,"fork":false,"pushed_at":"2024-06-11T15:10:20.000Z","size":3980,"stargazers_count":61,"open_issues_count":0,"forks_count":13,"subscribers_count":6,"default_branch":"primary","last_synced_at":"2025-03-26T00:01:51.230Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JoshData.png","metadata":{"files":{"readme":"README.markdown","changelog":"ChangeLog","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-12-12T02:08:52.000Z","updated_at":"2024-02-12T03:30:11.000Z","dependencies_parsed_at":"2024-10-31T15:13:39.913Z","dependency_job_id":null,"html_url":"https://github.com/JoshData/praat-py","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshData%2Fpraat-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshData%2Fpraat-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshData%2Fpraat-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshData%2Fpraat-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JoshData","download_url":"https://codeload.github.com/JoshData/praat-py/tar.gz/refs/heads/primary","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248517039,"owners_count":21117368,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T06:38:30.221Z","updated_at":"2025-04-12T04:30:54.356Z","avatar_url":"https://github.com/JoshData.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Praat-Py: Extended Praat for Python Scripting\n\nBy Joshua Tauberer \u003chttp://razor.occams.info\u003e\n\nOriginally developed in 2009. Only vaguely being currently maintained.\n\n## Introduction\n\nPraat-Py is a custom build of [Praat](http://www.fon.hum.uva.nl/praat/), the\ncomputer program used by linguists for doing phonetic analysis on sound files,\nto allow for scripts to be written in the [Python](http://www.python.org)\nprogramming language, rather than in Praat's built-in language.\n\nThe motivation for this is to remove the need for Praat script writers to\nlearn an entirely new programming language if they already know Python.\nAdditionally, Python is a more complete programming language and so scripts of\nany complexity should be more easily written in Python. The goal isn't to make\nall scripts easier or shorter to write, but to make it possible to do some\ncomplex tasks more easily.\n\nAll of the functionality of Praat's scripting language is retained when using\nPython, except forms. Likewise, because the Python interpreter is used, all\nPython functionality is possible, such as using all Python modules that you\nhappen to have installed.\n\nPraat-Py is a replacement for the Praat executable file you download from\nthe Praat website. Because this project is no longer well maintained, there\nis no ready-to-go replacement file available. Praat-Py must be compiled from\nsource files. Instructions for compiling Praat-Py are given later.\n\nThis project is released under the same terms as Praat itself, the GPL v2 or\nlater.\n\n## Example\n\nThe following is an example script for Praat written in Python, and after\nis the corresponding script in Praat's script language. The script converts all\n.aif files in a directory into .wav format, resamping them in the process. The\nPraat script on the right is based on the script\n[here](http://www.helsinki.fi/~lennes/praat-\nscripts/public/convert_aiff_files_to_16kHz_wav.praat) by Mietta Lennes.\n\nPraat script using Praat-Py: \n\n    \n    #lang=python\n    from glob import glob\n    \n    aiffdir = \"/home/tauberer/\"\n    delete_AIFF_files_after_conversion = False\n    \n    for file in glob(aiffdir + \"*.aiff\") :\n       print \"Converting file\", file, \"...\"\n    \t\n       aiff = go(\"Read from file...\", file)\n       resampled = go(\"Resample...\", 16000, 50)\n       soundtype, soundname = aiff\n    \t\n       go(\"Write to WAV file...\", aiffdir + soundname + \".wav\")\n       if delete_AIFF_files_after_conversion :\n          go(\"filedelete\", file)\n    \t\t\n       remove(aiff)\n       remove(resampled)\n    \nThe original Praat script, which can be run either in Praat or\nin Praat-Py.\n\n      \n    Create Strings as file list... aifffiles 'aiffdir$'*.aif\n    numberOfFiles = Get number of strings\n    \n    for file to numberOfFiles\n       select Strings aifffiles\n       file$ = Get string... file\n       Read from file... 'aiffdir$''file$'\n       soundname$ = selected$ (\"Sound\", 1)\n       Rename... temp\n       printline Converting file 'file$'...\n       Resample... 16000 50\n       Rename... 'soundname$'\n       select Sound temp\n       Remove\n       select Sound 'soundname$'\n       Write to WAV file... 'aiffdir$''soundname$'.wav\n       if delete_AIFF_files_after_conversion = 1\n          filedelete 'aiffdir$''file$'\n       endif\n       Remove\n    endfor\n\n## Using Python in Praat-Py\n\n### Starting a Praat-Py Script\n\nOnce you have Praat-Py, you can write scripts in either Praat's built-in\nscripting language or in Python. You should be roughly familiar with Praat\nscripting before reading on (see [this\npage](http://www.fon.hum.uva.nl/praat/manual/Scripting.html)), as well as\n[Python](http://www.python.org/). To script with Python, all you have to do is\nbegin your script code with this:\n\n\n    #lang=python\n\nThat is, the very first line of your script must be a hash (#) followed\nimmediately by \"lang=python\" in lowercase letters. This instructs Praat to\ninterpret the remainder of your script as Python code. Without this special\nline, the script is run as a normal Praat script.\n\n### \"Print\" and \"Go\" Functions\n\nThere are several ways your Python script can make Praat do things. The first\nway is via the `print` Python command, which will output a string to the Praat\nInfo window:\n\n\n    #lang=python\n    print 11500 * 20\n\nThe next way is to have Praat do one of its commands on a menu or the main\nwindow buttons. You do this with the `go(_command_, _arguments..._)` function.\n\n    \n    #lang=python\n    go(\"To Spectrogram...\", .005, maxfreq, .002, 20, \"Gaussian\")\n    go(\"View\")\n\nThe first argument to the `go` function is the name of a command on a Praat\nmenu to run. The remaning arguments are any options that that command takes,\nin the order that they show up in Praat. You'll notice that as compared to\nPraat Script, they way you put the arguments together is significantly more\nstraight-forward. To use variables, for instance, you just use them (no\nstrange quotes). In Praat Script, you never quote the final argument. Here,\nyou quote all arguments uniformly: strings are quoted, and nothing else, as in\nPython normally.\n\n`go(...)` returns the selected object after the command completes, or None if\nnothing is selected. This is particularly useful for commands that create new\nobjects in the Praat list. See `selected()` below. Here's an example:\n\n    \n    #lang=python\n    wav = go(\"Read from file...\", \"myfile.wav\")\n    spec = go(\"To Spectrogram...\", .005, maxfreq, .002, 20, \"Gaussian\")\n    select(wav)\n    part = go(\"Extract part...\", 0.0, 1.0, True)\n    remove(wav)\n    remove(spec)\n    remove(part)\n\nThe third way is to have Praat do something but to capture the result of what\nit does. You can capture numeric results with `getNum(_command_,\n_arguments..._)`:\n\n    \n    #lang=python\n    print \"The end time is: \", getNum(\"Get end time\")*1000, \" (ms)\"\n\nAs in regular Praat scripts, this works by capturing the first number the\ncommand would normally print to Praat's Info window. As with `go`, you can\nprovide any number of arguments to `getNum`. `getNum` returns a Python float\nvalue.\n\nThere is also a function `getString` which works like `getNum` but it returns\nthe entire output line sent to the Info window by the command as a Python\nstring value.\n\n    \n    #lang=python\n    print \"The end time is: \", getString(\"Get end time\")\n\n### Selection Functions\n\nSome additional commands are provided to make it easier to work with Praat's\nselected objects. `select(_object name1_, [_object name2_, ...])` selects one\nor more objects by their names. It's just a convenience; you can also use\n`go(\"select\", _objectname_)`. The argument(s) to select can be either Python\nstrings that have the name of the object, or a tuple of the object type and\nname separately: `select( (_type1_, _name1_) [, ...])` . Here are some\nexamples:\n\n    \n    #lang=python\n    select(\"Sound mysound\") # selects just a sound\n    select(\"Sound mysound\", \"TextGrid mysound\") # selects these two together (and unselects everything else)\n    select( (\"Sound\", \"mysound\") ) # same as above, might be useful in some cases, note the double parens!\n    select( (\"Sound\", \"mysound\"), (\"TextGrid\", \"mysound\") ) # same as above\n\nTo add or remove objects from the selection, use `plus` and `minus` as in\nPraat scripting. They work just the same as `select`:\n\n    \n    #lang=python\n    select(\"Sound mysound\") # selects just a sound\n    plus(\"TextGrid mysound\") # selects additionally the TextGrid\n    minus(\"Sound mysound\", \"TextGrid mysound\") # deselects them\n    minus( (\"Sound\", \"mysound\"), (\"TextGrid\", \"mysound\") ) # deselects them using the tuple form\n\n`selected()` is a function that returns the selected object. It returns a\n_tuple_ containing the type and name of the object separately. If nothing is\ncurrently selected, None is returned.\n\n    \n    #lang=python\n    select(\"Sound mysound\")\n    (type, name) = selected() # returns ('Sound', 'MySound')\n    \n    s = selected()\n    print s # prints \"('Sound', 'MySound')\"\n    print s[0] # prints the type of the object, \"Sound\"\n    print s[1] # prints the name of the object, \"MySound\"\n    minus(s) # deselects the currently selected object\n\n\n### Other Shortcuts\n\nBecause removing objects from the Praat objects list is such a common task in\nscripts that batch-process files, there is a shortcut for selecting and then\nremoving objects using `remove(object)`:\n\n    \n    #lang=python\n    wav = go(\"Read from file...\", \"myfile.wav\")\n    ...\n    remove(wav)\n       or\n    remove('Sound myfile')\n\n### Running the Script from the Command Line\n\nAs with Praat Scripts normally, you can run a script from the command-line\ndirectly (and never see Praat itself), besides running it from a script\nwindow. Say \"myscript.praatpy\" is a Praat-Py script, i.e. a Python program\nbeginning with \"#lang=python\". You can then run it as:\n\n    \n    praat-py myscript.praatpy\n\nYou can also pass command-line parameters to the script:\n\n    \n    praat-py myscript.praatpy arg1 arg2\n\n...and access them with `argv` similar to the usual Python sys.argv list. The\nfirst element in this list is the name of the script itself.\n\n    \n    #lang=python\n    print argv     # this prints [u'myscript.praatpy', u'arg1', u'arg2']  (they're Unicode strings, assumed UTF-8 on command line)\n\n## Building Praat-Py from Sources\n\nYou can build Praat-Py on any Unix platform... at least in principle. I build\nit on Ubuntu. YMMV elsewhere. I've also been able to compile it with mingw32\n(running on Linux) so that it can be run on Windows.\n\n1) You will need to have Python 2.5 or 2.6 installed and the \"python-dev\"\npackage (or equivalent) for your distribution, or an install from sources.\n\n2) Get the [Praat\nsources](http://www.fon.hum.uva.nl/praat/download_sources.html) and follow the\ninstructions to build Praat on your platform. (You don't really need to build\nPraat now, but it would be a really good idea to make sure it builds and runs\nbefore going forward.)\n\nFor instance, on Linux, with the latest version of Praat at the time of\nwriting:\n\n    \n    wget http://www.fon.hum.uva.nl/praat/praat5332_sources.tar.gz\n    tar -zxf praat5332_sources.tar.gz\n    cd sources_5332\n    ln -s makefiles/makefile.defs.linux ./makefile.defs\n    make\n    ./praat\n\n3) Clone this git repository into a directory called `scripting` inside the\nPraat sources directory:\n\n    # assuming you are still in the sources directory\n    git clone git://github.com/tauberer/praat-py.git scripting\n\n4) The Praat sources must be patched to build Praat-Py and to invoke the\nPython interpreter instead of the Praat script interpreter when the script\nbegins with the magic string \"#lang=python\". Do this to patch your Praat\nsource files:\n    \n    # assuming you are still in the sources directory\n    patch -p1 \u003c scripting/praat-py.patch\n    \n\n5) Build Praat (now Praat-Py).\n\n    \n    make\n    ./praat-py # it runs!\n    \n\nThe `praat-py` executable in the current directory will now support Python\nscripts.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshdata%2Fpraat-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoshdata%2Fpraat-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshdata%2Fpraat-py/lists"}