-
Notifications
You must be signed in to change notification settings - Fork 0
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
ponzu0147
wants to merge
13
commits into
main
Choose a base branch
from
fix/compilation-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
63b8275
デザインの変更
ponzu0147 9aa376f
Engineファイルの移動
ponzu0147 c34a4b3
ファイル整理
ponzu0147 db2df19
ワークアドレス仮実装
ponzu0147 339aac8
謎
ponzu0147 a23f9be
IPL実装
ponzu0147 dc12756
D88ディスクイメージ読み込み機能の実装
ponzu0147 4d2b175
BugFix
ponzu0147 45fce27
640x400の画面表示実装
ponzu0147 56c41a3
feat: D88ディスクからのIPLロード機能を実装
ponzu0147 388501d
M88エミュレータのフォントROM、BIOS、リズム音色サンプルを実装
ponzu0147 99629ae
Fix compilation errors in PC88BIOS and related classes
ponzu0147 cd91a89
D88ファイル自動解析機能の実装と修正
ponzu0147 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
} | ||
|
||
// 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("🔊 オーディオエンジン強制停止完了") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.