| 306 | } |
| 307 | |
| 308 | void Sound::determineRootKey() |
| 309 | { |
| 310 | // Try to find the root key with the help of the file name |
| 311 | QFileInfo fileInfo(_fileName); |
| 312 | QString nameNoExtension = fileInfo.completeBaseName().toLower(); |
| 313 | |
| 314 | // First attempt: find a note name (standard taken being C4 = 60) |
| 315 | static QRegularExpression keyNameRegex("([a-g])([#b]?)(-?[0-9]+)"); |
| 316 | bool keyFound = false; |
| 317 | unsigned int key = 0; |
| 318 | QRegularExpressionMatch match = keyNameRegex.match(nameNoExtension); |
| 319 | if (match.hasMatch()) |
| 320 | { |
| 321 | QString captured = match.captured(1); |
| 322 | switch (captured[0].toLatin1()) |
| 323 | { |
| 324 | case 'c': |
| 325 | key = 0; |
| 326 | break; |
| 327 | case 'd': |
| 328 | key = 2; |
| 329 | break; |
| 330 | case 'e': |
| 331 | key = 4; |
| 332 | break; |
| 333 | case 'f': |
| 334 | key = 5; |
| 335 | break; |
| 336 | case 'g': |
| 337 | key = 7; |
| 338 | break; |
| 339 | case 'a': |
| 340 | key = 9; |
| 341 | break; |
| 342 | case 'b': |
| 343 | key = 11; |
| 344 | break; |
| 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | if (match.captured(2) == "b") |
| 349 | key--; |
| 350 | else if (match.captured(2) == "#") |
| 351 | key++; |
| 352 | key += 12 * (1 + match.captured(3).toInt()); |
| 353 | |
| 354 | if (key > 0 && key < 128) |
| 355 | keyFound = true; |
| 356 | } |
| 357 | |
| 358 | // Second attempt: search the numeric name of the key |
| 359 | if (!keyFound) |
| 360 | { |
| 361 | static QRegularExpression reg = QRegularExpression("[^0-9]"); |
| 362 | QStringList listeNum = nameNoExtension.replace(reg, "-").split("-", Qt::SkipEmptyParts); |
| 363 | if (!listeNum.isEmpty()) |
| 364 | { |
| 365 | // We study the last part |