Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Extracting a key/tonality from a filename using [regexp]

$
0
0

I'm trying to find a way to extract the key from the file name, bypassing all min/maj characters (case insensitive), but allowing # and b.

A couple of file name examples:

sample_F_one_shot.wavsample_oneshot_F#maj.wavsample_Ab_one_shot.wav

An example of what the result should look like:

A; B; C; D; E; F; G

and their combinations with # and b.

Here are the basic rules:

  • The key is not case sensitive and can be written in small or capital letters.
  • The key is not at the beginning of the file name.
  • The key can be in brackets: "()"; "[]".
  • There may be signs before the key: ""; ""; "."; " "; "space"; "-"; "--"; "*"; "-".
  • After the key there may be signs: ""; ""; "."; " "; "space"; "-"; "--"; "*"; "-"; ".wav"; ",aif"; ".rx2".
  • There cannot be letters before the key.
  • There cannot be numbers before the key.
  • After the key there may be numbers: “C1”; "F#2" - numbers must be cut off.
  • Possible options with: "#maj"; "#Maj"; "#min"; "#Min"; "#m" - everything after "#" should be cut off

I came up with a 2 step system using [regexp]. In the first step I find the key with symbols around it, for example: F; .F.; [F]; F#; _F#maj; and so on...

Something like:

"regexp (?:[^a-gA-G#b]|^)([A-G](?:#|b)?(?:m(?:in(?:or)?)?|M(?:aj(?:or)?)?))(?:[^a-zA-Z#bd]|$)"

The second step is to simply get rid of all the characters, leaving only # and b in the key using another [regexp].

Problem: In the first step I am unable to achieve high accuracy in isolating the key with symbols.

Question: Perhaps there is an easier way to do this?

I think I'm confused about this. I also tried to do this using ChatGPT and it seems to be the best option I got, but it is not entirely accurate.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>