替换文本以更改 pitch/key

Replacing text in order to change pitch/key

我的代码有很大的困难。我正在开发一个显示歌词和歌曲和弦的应用程序。我使用两个重叠的文本视图将和弦和歌词分开。

我在这个项目中遇到的问题是变调功能。我会尽力解释我自己:

一共12个和弦:Do-Do#-Re-Re#-Mi-Fa-Fa#-Sol-Sol#-La-La#-Si

我使用两个按钮 + 和 - 来改变音高

为了包含从一个和弦到另一个和弦的空格,我以这种方式使用了 replacingOccurrences(of:with:options:) 实例方法:

//MARK: - Change pitch 
    @IBAction func risePitch(_ sender: UIButton) {

                //positive side was pressed
                let dosre = chords.text.replacingOccurrences(of: "Do#", with: "Re", options: .widthInsensitive)
                chords.text = dosre

                let dodos = chords.text.replacingOccurrences(of: "Do", with: "Do#", options: .widthInsensitive)
                chords.text = dodos

                let resmi = chords.text.replacingOccurrences(of: "Re#", with: "Mi", options: .widthInsensitive)
                chords.text = resmi

                let remi = chords.text.replacingOccurrences(of: "Re", with: "Re#", options: .widthInsensitive)
                chords.text = remi

                let fassol = chords.text.replacingOccurrences(of: "Fa#", with: "Sol", options: .widthInsensitive)
                chords.text = fassol

                let mifa = chords.text.replacingOccurrences(of: "Mi", with: "Fa", options: .widthInsensitive)
                chords.text = mifa

                let fafas = chords.text.replacingOccurrences(of: "Fa", with: "Fa#", options: .widthInsensitive)
                chords.text = fafas

                let solsla = chords.text.replacingOccurrences(of: "Sol#", with: "La", options: .widthInsensitive)
                chords.text = solsla

                let solsols = chords.text.replacingOccurrences(of: "Sol", with: "Sol#", options: .widthInsensitive)
                chords.text = solsols

                let lassi = chords.text.replacingOccurrences(of: "La#", with: "Si", options: .widthInsensitive)
                chords.text = lassi

                let lalas = chords.text.replacingOccurrences(of: "La", with: "La#", options: .widthInsensitive)
                chords.text = lalas

                let sido = chords.text.replacingOccurrences(of: "Si", with: "Do", options: .widthInsensitive)
                chords.text = sido

    }


    @IBAction func decreasePitch(_ sender: UIButton) {
        //negative side was pressed
        let dosre = chords.text.replacingOccurrences(of: "Do#", with: "Do", options: .widthInsensitive)
        chords.text = dosre

        let dore = chords.text.replacingOccurrences(of: "Do", with: "Si", options: .widthInsensitive)
        chords.text = dore

        let resmi = chords.text.replacingOccurrences(of: "Re#", with: "Re", options: .widthInsensitive)
        chords.text = resmi

        let remi = chords.text.replacingOccurrences(of: "Re", with: "Do#", options: .widthInsensitive)
        chords.text = remi

        let mifa = chords.text.replacingOccurrences(of: "Mi", with: "Re#", options: .widthInsensitive)
        chords.text = mifa

        let fassol = chords.text.replacingOccurrences(of: "Fa#", with: "Fa", options: .widthInsensitive)
        chords.text = fassol

        let fafas = chords.text.replacingOccurrences(of: "Fa", with: "Mi", options: .widthInsensitive)
        chords.text = fafas

        let solsla = chords.text.replacingOccurrences(of: "Sol#", with: "Sol", options: .widthInsensitive)
        chords.text = solsla

        let solsols = chords.text.replacingOccurrences(of: "Sol", with: "Fa#", options: .widthInsensitive)
        chords.text = solsols

        let lassi = chords.text.replacingOccurrences(of: "La#", with: "La", options: .widthInsensitive)
        chords.text = lassi

        let lalas = chords.text.replacingOccurrences(of: "La", with: "Sol#", options: .widthInsensitive)
        chords.text = lalas

        let sido = chords.text.replacingOccurrences(of: "Si", with: "La#", options: .widthInsensitive)
        chords.text = sido


    }

如果您运行此代码,您会发现音高转换无法正常工作。

希望我已经足够清楚....

如果你把音符分成一个字符串数组(并有一个临时数组来进行编辑),然后将升高和降低音调功能分别 +1 或 -1 会容易得多每次注意。然后您可以将和弦数组折叠成一个字符串来显示它。此代码在这里有效:

var masterChords = ["Do",  "Do#",  "Re",  "Re#",  "Mi",  "Fa",  "Fa#",  "Sol",  "Sol#",  "La",  "La#",  "Si"]
var chords = ["Do", "Sol", "Mi"]

func raisePitch() {
    for i in 0...chords.count - 1 {
        for j in 0...masterChords.count - 1 {
            if chords[i] == masterChords[j] {
                if j < masterChords.count - 1 {
                    chords[i] = masterChords[j + 1]
                    break
                } else {
                    chords[i] = masterChords[0]
                    break
                }
            }
        }
    }
}

func lowerPitch() {
    for i in 0...chords.count - 1 {
        for j in 0...masterChords.count - 1 {
            if chords[i] == masterChords[j] {
                if j > 0 {
                    chords[i] = masterChords[j - 1]
                    break
                } else {
                    chords[i] = masterChords[masterChords.count - 1]
                    break
                }
            }
        }
    }
}


//Use the code below to test
print(chords)

raisePitch()

print(chords)

lowerPitch()

print(chords)

lowerPitch()

print(chords)