timmahrt / praatIO

A python library for working with praat, textgrids, time aligned audio transcripts, and audio files. It is primarily used for extracting features from and making manipulations on audio files given hierarchical time-aligned transcriptions (utterance > word > syllable > phone, etc).
MIT License
299 stars 32 forks source link

Json or csv format? #21

Closed mirfan899 closed 3 years ago

mirfan899 commented 3 years ago

Is there a way to save textgrid file to csv or json format?

timmahrt commented 3 years ago

There is not, but I think its something that would be useful to add.

What would it look like?

I have a textgridToCsv function in my praatIOjs library. However, it is lossy. You choose an annotation tier and it will save the textgrid information from the perspective of that tier. It powers this page: https://timmahrt.github.io/web_transcription_tools/dist/tg_to_csv.html#/

Alternatively, it could exhaustively list all data with a format like: tier_name, INTERVAL, start, stop tier_name, POINT, label, time

Json would look similar to the textgrid format but formatted for json.

I should be able to work on this over the weekend. 👍 If you have input on what would be helpful, please share.

timmahrt commented 3 years ago

Is there an existing library/program that reads json or csv audio annotations that you're targeting? I could test out my work with it directly.

mirfan899 commented 3 years ago

Nope according to my googling. I'm working on comparing the phonemes of audio to check its pronunciations.

timmahrt commented 3 years ago

Ok, I've made a new release: v4.2.0

If you update praatio, you should be able to load and save Textgrids as json files in praatio. It uses the existing saving/loading mechanisms, but with a flag to specify that json should be used. By default, it uses the legacy behaviour.

Here is how to trigger the new behaviour:

tg = openTextgrid("blah.json", readAsJson=True)
tg.save("blah2.json", outputFormat=tgio.JSON)

Ah, I just forgot, I should probably document the json format somewhere? It's pretty simple:

{
"xmin": (global min value)
"xmax": (global max value)
"tiers": [
  {
    "class": ("IntervalTier"|"PointTier")
    "name": (user given name)
    "xmin": (tier min value)
    "xmax": (tier max value)
    "entries": [
     [start, stop, label], # for interval tier's entries
     [start, label], # for point tier's entries
    ]
  }
]

I wouldn't be surprised if there were some bugs or bad coding decisions. Please let me know if you have any feedback. Thank you!