File indexing completed on 2024-05-19 04:56:01

0001 /**
0002  * \file eventtimingcode.cpp
0003  * Event timing code to string conversion.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 21 Mar 2014
0008  *
0009  * Copyright (C) 2014-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "eventtimingcode.h"
0028 
0029 #include <QCoreApplication>
0030 
0031 namespace {
0032 
0033 const struct {
0034   const char* text;
0035   int code;
0036 } codes[] = {
0037   { QT_TRANSLATE_NOOP("@default", "padding (has no meaning)"), 0x00 },
0038   { QT_TRANSLATE_NOOP("@default", "end of initial silence"), 0x01 },
0039   { QT_TRANSLATE_NOOP("@default", "intro start"), 0x02 },
0040   { QT_TRANSLATE_NOOP("@default", "main part start"), 0x03 },
0041   { QT_TRANSLATE_NOOP("@default", "outro start"), 0x04 },
0042   { QT_TRANSLATE_NOOP("@default", "outro end"), 0x05 },
0043   { QT_TRANSLATE_NOOP("@default", "verse start"), 0x06 },
0044   { QT_TRANSLATE_NOOP("@default", "refrain start"), 0x07 },
0045   { QT_TRANSLATE_NOOP("@default", "interlude start"), 0x08 },
0046   { QT_TRANSLATE_NOOP("@default", "theme start"), 0x09 },
0047   { QT_TRANSLATE_NOOP("@default", "variation start"), 0x0a },
0048   { QT_TRANSLATE_NOOP("@default", "key change"), 0x0b },
0049   { QT_TRANSLATE_NOOP("@default", "time change"), 0x0c },
0050   { QT_TRANSLATE_NOOP("@default", "momentary unwanted noise (Snap, Crackle & Pop)"), 0x0d },
0051   { QT_TRANSLATE_NOOP("@default", "sustained noise"), 0x0e },
0052   { QT_TRANSLATE_NOOP("@default", "sustained noise end"), 0x0f },
0053   { QT_TRANSLATE_NOOP("@default", "intro end"), 0x10 },
0054   { QT_TRANSLATE_NOOP("@default", "main part end"), 0x11 },
0055   { QT_TRANSLATE_NOOP("@default", "verse end"), 0x12 },
0056   { QT_TRANSLATE_NOOP("@default", "refrain end"), 0x13 },
0057   { QT_TRANSLATE_NOOP("@default", "theme end"), 0x14 },
0058   { QT_TRANSLATE_NOOP("@default", "profanity"), 0x15 },
0059   { QT_TRANSLATE_NOOP("@default", "profanity end"), 0x16 },
0060   { QT_TRANSLATE_NOOP("@default", "not predefined synch 0"), 0xe0 },
0061   { QT_TRANSLATE_NOOP("@default", "not predefined synch 1"), 0xe1 },
0062   { QT_TRANSLATE_NOOP("@default", "not predefined synch 2"), 0xe2 },
0063   { QT_TRANSLATE_NOOP("@default", "not predefined synch 3"), 0xe3 },
0064   { QT_TRANSLATE_NOOP("@default", "not predefined synch 4"), 0xe4 },
0065   { QT_TRANSLATE_NOOP("@default", "not predefined synch 5"), 0xe5 },
0066   { QT_TRANSLATE_NOOP("@default", "not predefined synch 6"), 0xe6 },
0067   { QT_TRANSLATE_NOOP("@default", "not predefined synch 7"), 0xe7 },
0068   { QT_TRANSLATE_NOOP("@default", "not predefined synch 8"), 0xe8 },
0069   { QT_TRANSLATE_NOOP("@default", "not predefined synch 9"), 0xe9 },
0070   { QT_TRANSLATE_NOOP("@default", "not predefined synch A"), 0xea },
0071   { QT_TRANSLATE_NOOP("@default", "not predefined synch B"), 0xeb },
0072   { QT_TRANSLATE_NOOP("@default", "not predefined synch C"), 0xec },
0073   { QT_TRANSLATE_NOOP("@default", "not predefined synch D"), 0xed },
0074   { QT_TRANSLATE_NOOP("@default", "not predefined synch E"), 0xee },
0075   { QT_TRANSLATE_NOOP("@default", "not predefined synch F"), 0xef },
0076   { QT_TRANSLATE_NOOP("@default", "audio end (start of silence)"), 0xfd },
0077   { QT_TRANSLATE_NOOP("@default", "audio file ends"), 0xfe }
0078 };
0079 
0080 constexpr int numCodes = std::size(codes);
0081 
0082 }
0083 
0084 /**
0085  * Get string representation.
0086  * @return code description.
0087  */
0088 QString EventTimeCode::toString() const
0089 {
0090   for (int i = 0; i < numCodes; ++i) {
0091     if (codes[i].code == m_code) {
0092       return QString::fromLatin1(codes[i].text);
0093     }
0094   }
0095   return QString(QLatin1String("reserved for future use %1"))
0096       .arg(m_code, 2, 16, QLatin1Char('0'));
0097 }
0098 
0099 /**
0100  * Get translated string representation.
0101  * @return code description.
0102  */
0103 QString EventTimeCode::toTranslatedString() const
0104 {
0105   for (int i = 0; i < numCodes; ++i) {
0106     if (codes[i].code == m_code) {
0107       return QCoreApplication::translate("@default", codes[i].text);
0108     }
0109   }
0110   const char* const reservedForFutureUseStr =
0111       QT_TRANSLATE_NOOP("@default", "reserved for future use %1");
0112   return QCoreApplication::translate("@default", reservedForFutureUseStr)
0113       .arg(m_code, 2, 16, QLatin1Char('0'));
0114 }
0115 
0116 /**
0117  * Get index of code in list of strings.
0118  * @return index.
0119  */
0120 int EventTimeCode::toIndex() const
0121 {
0122   for (int i = 0; i < numCodes; ++i) {
0123     if (codes[i].code == m_code) {
0124       return i;
0125     }
0126   }
0127   return -1;
0128 }
0129 
0130 /**
0131  * Create from string.
0132  * @param str untranslated string
0133  * @return event time code.
0134  */
0135 EventTimeCode EventTimeCode::fromString(const char* str)
0136 {
0137   for (int i = 0; i < numCodes; ++i) {
0138     if (qstrcmp(codes[i].text, str) == 0) {
0139       return EventTimeCode(codes[i].code);
0140     }
0141   }
0142   return EventTimeCode(-1);
0143 }
0144 
0145 /**
0146  * Create from index.
0147  * @param index index
0148  * @return event time code.
0149  */
0150 EventTimeCode EventTimeCode::fromIndex(int index)
0151 {
0152   if (index >= 0 && index < numCodes) {
0153     return EventTimeCode(codes[index].code);
0154   }
0155   return EventTimeCode(-1);
0156 }
0157 
0158 /**
0159  * Get list of translated strings.
0160  * @return code descriptions.
0161  */
0162 QStringList EventTimeCode::getTranslatedStrings()
0163 {
0164   QStringList strings;
0165   strings.reserve(numCodes);
0166   for (int i = 0; i < numCodes; ++i) {
0167     strings << QCoreApplication::translate("@default", codes[i].text);
0168   }
0169   return strings;
0170 }