Skip to content

D88ファイル自動解析機能の実装とコンパイルエラーの修正 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Empty file added .windsurfrules
Empty file.
153 changes: 153 additions & 0 deletions AudioEngine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 完全に再設計された停止メソッド
func stop() {
print("🔊 オーディオエンジン停止開始")

// 現在の状態をログ出力
print("🔊 停止前の状態: isRunning=\(isRunning), playerNode.isPlaying=\(playerNode?.isPlaying ?? false), engine.isRunning=\(engine.isRunning)")

// 再生状態をオフに(最初に設定)
isRunning = false

// メインスレッドで強制的に実行
DispatchQueue.main.async { [self] in
// 1. プレイヤーノードを強制的に停止・解放
if let player = playerNode {
player.pause()
player.stop()
player.reset()
print("🔊 プレイヤーノード停止完了")

// すべてのバッファをキャンセル
player.reset()

// エンジンから切り離す(重要)
engine.detach(player)
playerNode = nil
}

// 2. エンジンを完全に停止
engine.stop()

// 3. すべてのノードを切断
engine.reset()

// 4. アプリケーションレベルの処理
do {
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
} catch {
print("⚠️ オーディオセッション停止エラー: \(error)")
}
Comment on lines +35 to +39

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding more specific error handling here. Knowing the exact error that occurred during audio session activation can be very helpful for debugging. For example, you could log the localizedDescription of the error.

        } catch let error as NSError { // Capture the NSError to access its code
            print("⚠️ AudioSession停止エラー: \(error), domain: \(error.domain), code: \(error.code)")
        }


// 5. 音源エンジンをリセット
ssgEngine = SSGEngine(sampleRate: sampleRate, cpuClock: cpuClock)
fmEngine = FMEngine(sampleRate: sampleRate, fmClock: cpuClock)
rhythmEngine = RhythmEngine(sampleRate: sampleRate)
adpcmEngine = ADPCMEngine(sampleRate: sampleRate, adpcmClock: cpuClock)

print("🔊 オーディオエンジン完全停止・リセット完了")
}
}

private func completeStop() {
// 現在のプレイヤーノードを停止
if let player = playerNode {
// 再生中なら停止(強制的に実行)
player.stop()
print("🔊 プレイヤーノード停止")

// バッファをリセット
player.reset()

// エンジンから切り離す
engine.detach(player)
playerNode = nil
print("🔊 プレイヤーノード解放")
}

// エンジンを停止
do {
// 状態にかかわらず強制的に停止
engine.stop()
print("🔊 AVAudioEngine停止")

// エンジンを完全にリセット
engine.reset()
print("🔊 AVAudioEngineリセット完了")

// オーディオセッションを非アクティブにする
try AVAudioSession.sharedInstance().setActive(false)
print("🔊 オーディオセッション非アクティブ化")
} catch {
print("⚠️ オーディオエンジン停止エラー: \(error)")
}
}

func recreateEngine() {
// 古いエンジンを破棄
engine.stop()
engine.reset()

// 新しいエンジンを作成
engine = AVAudioEngine()
setupEngine()
}

func applicationWillResignActive() {
// アプリがバックグラウンドに移行する際に強制停止
stop()
}

// 停止ボタンのデバッグ用メソッド
func debugStopButton() {
print("🔍 停止ボタン押下検出!現在の状態:")
print("- isRunning: \(isRunning)")
print("- engine.isRunning: \(engine.isRunning)")
print("- playerNode?.isPlaying: \(playerNode?.isPlaying ?? false)")

// 強制停止試行
stop()

// 状態確認
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
guard let self = self else { return }
print("🔍 停止処理後の状態:")
print("- isRunning: \(self.isRunning)")
print("- engine.isRunning: \(self.engine.isRunning)")
print("- playerNode?.isPlaying: \(self.playerNode?.isPlaying ?? false)")
}
}

// 強制停止用のメソッド追加
func forceStop() {
print("🔊 オーディオエンジン強制停止開始")

// すべてのフラグをオフに
isRunning = false

// AVAudioEngineを直接停止
if engine.isRunning {
engine.stop()
print("🔊 AVAudioEngine強制停止")
}

// プレイヤーノードを強制停止
if let player = playerNode {
if player.isPlaying {
player.stop()
print("🔊 プレイヤーノード強制停止")
}
}

// オーディオセッション終了
do {
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
print("🔊 オーディオセッション強制終了")
} catch {
print("⚠️ オーディオセッション終了エラー: \(error)")
}

// 音源エンジンのリセット
resetAllEngines()

print("🔊 オーディオエンジン強制停止完了")
}
217 changes: 217 additions & 0 deletions FMEngine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
class FMEngine {
// 既存のプロパティ
private var registers: [UInt8]
private let sampleRate: Float
private let fmClock: Float

// 追加が必要なプロパティ
private var phases: [Float] = Array(repeating: 0.0, count: 6) // 各チャンネルの位相
private var envelopes: [Float] = Array(repeating: 1.0, count: 6) // エンベロープ状態
private var channelKeyOnState: [Bool] = Array(repeating: false, count: 6) // 各チャンネルのキーオン状態

// 初期化
init(registers: [UInt8], sampleRate: Float, fmClock: Float = 3993600.0) {
self.registers = registers
self.sampleRate = sampleRate
self.fmClock = fmClock

// デバッグ出力
print("🎹 FMエンジン初期化: registers.count = \(registers.count), sampleRate = \(sampleRate)")
}

// レジスタ更新
func updateRegisters(_ newRegisters: [UInt8]) {
// キーオン状態の変化を検出
let oldKeyOnReg = registers[0x28]
let newKeyOnReg = newRegisters[0x28]

if oldKeyOnReg != newKeyOnReg {
// キーオン状態が変化した場合、チャンネル状態を更新
updateKeyOnState(newKeyOnReg)
}

// レジスタを更新
self.registers = newRegisters
}

// キーオン状態の更新
private func updateKeyOnState(_ keyOnReg: UInt8) {
// スロットマスク(どのオペレータがONか)
let slotMask = (keyOnReg >> 4) & 0x0F

// チャンネル番号とグループを取得
let chNum = keyOnReg & 0x03
let isSecondGroup = (keyOnReg & 0x04) != 0
let actualChannel = isSecondGroup ? chNum + 3 : chNum

// このチャンネルがキーオンされているか確認
let isKeyOn = slotMask != 0

// 状態変化をログ出力
let oldState = channelKeyOnState[Int(actualChannel)]
if oldState != isKeyOn {
print("🔑 CH\(actualChannel) キーオン状態変化: \(oldState ? "オン" : "オフ") -> \(isKeyOn ? "オン" : "オフ"), スロットマスク: 0x\(String(format: "%02X", slotMask))")
}

// 状態を更新
channelKeyOnState[Int(actualChannel)] = isKeyOn
}

// generateSampleメソッドの改善
func generateSample(_ timeStep: Float) -> Float {
// アクティブなチャンネルを確認
var hasActiveChannel = false
var activeChannels = [Int]()

for ch in 0..<6 {
if isChannelActive(ch) {
hasActiveChannel = true
activeChannels.append(ch)
}
}

// デバッグ出力(サンプル生成前)
if Int.random(in: 0..<1000) < 5 {
let keyOnReg = registers[0x28]
print("🎹 キーオン: 0x\(String(format: "%02X", keyOnReg)), アクティブチャンネル: \(activeChannels)")
}

// アクティブなチャンネルがない場合は0を返す
if !hasActiveChannel {
return 0.0
}

// 各チャンネルの出力を合成
var output: Float = 0.0

for ch in activeChannels {
// チャンネルごとの位相を更新
phases[ch] += getFrequency(ch) * timeStep
if phases[ch] >= 1.0 {
phases[ch] -= 1.0
}

// エンベロープ計算
envelopes[ch] = calculateEnvelope(ch)

// アルゴリズムに基づいて波形を生成
let waveform = generateWaveform(ch, phases[ch])

// 音量適用
let channelOutput = waveform * envelopes[ch] * getChannelVolume(ch)
output += channelOutput

// サンプル生成のデバッグ(非常に低頻度)
if Int.random(in: 0..<10000) < 5 {
print("🎵 CH\(ch) 波形生成: 位相=\(phases[ch]), 波形=\(waveform), エンベロープ=\(envelopes[ch]), 出力=\(channelOutput)")
}
}

// 非ゼロ出力の場合はデバッグログ
if abs(output) > 0.01 && Int.random(in: 0..<1000) < 10 {
print("🔊 FM出力: \(output), アクティブチャンネル: \(activeChannels.count)個")
}

return output
}

// チャンネルがアクティブかどうか判定
private func isChannelActive(_ ch: Int) -> Bool {
// 保存されたキーオン状態を使用
if !channelKeyOnState[ch] {
return false
}

// チャンネルパラメータも確認
let fnum = getChannelFnum(ch)
let block = getChannelBlock(ch)

// 追加のデバッグ情報(低頻度)
if Int.random(in: 0..<10000) < 1 {
print("🔍 CH\(ch) 状態: キーオン=\(channelKeyOnState[ch]), FNUM=\(fnum), BLOCK=\(block)")
}

// FNUMが0でなければアクティブと判断
return fnum > 0
}

// チャンネルのFNUM値を取得
private func getChannelFnum(_ ch: Int) -> Int {
let chOffset = ch % 3
let baseAddr = ch < 3 ? 0xA0 : 0x1A0

let fnumL = registers[baseAddr + chOffset]
let fnumH = registers[baseAddr + 4 + chOffset]

return (Int(fnumH & 0x07) << 8) | Int(fnumL)
}

// チャンネルのBLOCK値を取得
private func getChannelBlock(_ ch: Int) -> Int {
let chOffset = ch % 3
let baseAddr = ch < 3 ? 0xA4 : 0x1A4

return Int((registers[baseAddr + chOffset] >> 3) & 0x07)
}

// 周波数計算
private func getFrequency(_ ch: Int) -> Float {
let fnum = getChannelFnum(ch)
let block = getChannelBlock(ch)

// OPNA FM周波数計算式
let freq = Float(fnum) * pow(2, Float(block)) * (fmClock / (144.0 * 2048.0))
return freq / sampleRate
}

// 波形生成
private func generateWaveform(_ ch: Int, _ phase: Float) -> Float {
let chOffset = ch % 3
let baseAddr = ch < 3 ? 0xB0 : 0x1B0

let algFB = registers[baseAddr + chOffset]
let algorithm = algFB & 0x07
let feedback = (algFB >> 3) & 0x07

// アルゴリズムに基づいて波形を生成(簡略化)
// 実際のFM合成はもっと複雑ですが、簡略化のため単純な正弦波で実装
let waveform = sin(2.0 * Float.pi * phase)

// フィードバック量に応じて波形を歪ませる(簡略化)
let feedbackAmount = Float(feedback) / 7.0
if feedbackAmount > 0 {
return waveform * (1.0 + feedbackAmount * 0.2 * sin(4.0 * Float.pi * phase))
} else {
return waveform
}
}

// エンベロープ計算
private func calculateEnvelope(_ ch: Int) -> Float {
// TL (Total Level) を取得
let chOffset = ch % 3
let baseAddr = ch < 3 ? 0x40 : 0x140

// 4つのオペレータのTL値を取得して逆変換(0が最大音量、127が最小音量)
let op1TL = Float(registers[baseAddr + chOffset])
let op2TL = Float(registers[baseAddr + 8 + chOffset])
let op3TL = Float(registers[baseAddr + 4 + chOffset])
let op4TL = Float(registers[baseAddr + 12 + chOffset])

// TLは減衰値のため、127から引いて0-127の範囲にし、それを127で割って0-1に正規化
let env1 = (127.0 - op1TL) / 127.0
let env2 = (127.0 - op2TL) / 127.0
let env3 = (127.0 - op3TL) / 127.0
let env4 = (127.0 - op4TL) / 127.0

// アルゴリズムに基づいて適切なエンベロープを返す
// 簡略化のため、ここではアルゴリズム7(全オペレータ並列)を想定
return (env1 + env2 + env3 + env4) * 0.25
}

// チャンネル音量の取得
private func getChannelVolume(_ ch: Int) -> Float {
// 音量を上げる(テスト用)
return 0.8
}
}
Loading