gpt4 book ai didi

ios - 替换文本以更改音高/调

转载 作者:行者123 更新时间:2023-12-01 21:55:43 26 4
gpt4 key购买 nike

我的代码遇到了很大的困难。我正在开发一个显示歌词和和弦的应用程序。我使用两个重叠的textview分隔了和弦和歌词。

我在这个项目中遇到的问题是音高改变功能。我尽我所能向我解释得更好:

和弦总数为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)

关于ios - 替换文本以更改音高/调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61216673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com