From cc056b2d2c9b7e0aacf5facf2003178bd4be5195 Mon Sep 17 00:00:00 2001 From: flswld Date: Tue, 16 Jul 2024 11:14:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=BA=E6=99=AF=E9=9F=B3=E9=A2=91=E6=94=AF?= =?UTF-8?q?=E6=8C=81midi=E9=94=AE=E7=9B=98=E8=AE=BE=E5=A4=87=E8=BE=93?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gs/game/audio_video.go | 52 +++++++++++++++-------------------- gs/game/audio_video_rtmidi.go | 8 ++++++ gs/game/game_command_gm.go | 16 +++++------ gs/game/game_tick_manager.go | 4 +-- gs/game/player_common.go | 6 +++- 5 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 gs/game/audio_video_rtmidi.go diff --git a/gs/game/audio_video.go b/gs/game/audio_video.go index ed9b1c2b..a1c9b73b 100644 --- a/gs/game/audio_video.go +++ b/gs/game/audio_video.go @@ -16,7 +16,6 @@ import ( "hk4e/protocol/proto" "gitlab.com/gomidi/midi/v2" - _ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv" "gitlab.com/gomidi/midi/v2/smf" ) @@ -26,31 +25,21 @@ const ( ) var ( - AUDIO_CHAN = make(chan uint32, 1000) - MidiInputDevStopListenFunc func() = nil + AudioChan = make(chan uint32, 1000) + MidiInputDevStop func() = nil ) func sendMidiMsg(msg midi.Message) { - if msg.Type() != midi.NoteOnMsg { - return - } var channel, key, velocity uint8 - msg.GetNoteOn(&channel, &key, &velocity) - // TODO 测试一下客户端是否支持更宽的音域 + if !msg.GetNoteStart(&channel, &key, &velocity) { + return + } // 60 -> 中央C C4 - // if key < 36 || key > 71 { - // continue - // } note := int32(key) + int32(KeyOffset) - if note < 21 || note > 108 { - // 非88键钢琴音域 + if note < 36 || note > 96 { return } - if velocity == 0 { - // 可能是NoteOffMsg - return - } - AUDIO_CHAN <- uint32(note) + AudioChan <- uint32(note) } func PlayAudio(fileData []byte) { @@ -69,17 +58,17 @@ func PlayAudio(fileData []byte) { metricTicks := audio.TimeFormat.(smf.MetricTicks) tickTime := ((60000000.0 / tempoChange.BPM) / float64(metricTicks.Resolution())) / 1000.0 logger.Debug("start play audio") + // 全部轨道 for _, track := range audio.Tracks { - // 全部轨道 - totalTick := uint64(0) - for _, event := range track { - // 单个轨道 - delay := uint32(float64(event.Delta) * tickTime) - // busyPollWaitMilliSecond(delay) - interruptWaitMilliSecond(delay) - totalTick += uint64(delay) - sendMidiMsg(midi.Message(event.Message)) - } + // 单个轨道 + go func(track smf.Track) { + for _, event := range track { + delay := uint32(float64(event.Delta) * tickTime) + // busyPollWaitMilliSecond(delay) + interruptWaitMilliSecond(delay) + sendMidiMsg(midi.Message(event.Message)) + } + }(track) } } @@ -104,7 +93,7 @@ func StartMidiInputDev() error { if err != nil { return err } - MidiInputDevStopListenFunc, err = midi.ListenTo(in, func(msg midi.Message, timestampms int32) { + MidiInputDevStop, err = midi.ListenTo(in, func(msg midi.Message, timestampms int32) { logger.Debug("midi input dev msg: %v", msg) sendMidiMsg(msg) }) @@ -115,7 +104,7 @@ func StartMidiInputDev() error { } func StopMidiInputDev() { - MidiInputDevStopListenFunc() + MidiInputDevStop() midi.CloseDriver() } @@ -124,6 +113,7 @@ const ( SCREEN_HEIGHT = 80 SCREEN_DPI = 0.5 ) + const GADGET_ID = 70590015 var SCREEN_ENTITY_ID_LIST []uint32 @@ -139,6 +129,7 @@ const ( GADGET_CYAN_BLUE = 70590018 GADGET_PURPLE = 70590020 ) + const ( RED_RGB = "C3764F" GREEN_RGB = "559F30" @@ -158,6 +149,7 @@ var COLOR_GADGET_MAP = map[string]int{ CYAN_BLUE_RGB: GADGET_CYAN_BLUE, PURPLE_RGB: GADGET_PURPLE, } + var ALL_COLOR = []string{RED_RGB, GREEN_RGB, BLUE_RGB, CYAN_RGB, YELLOW_RGB, CYAN_BLUE_RGB, PURPLE_RGB} type ColorLight struct { diff --git a/gs/game/audio_video_rtmidi.go b/gs/game/audio_video_rtmidi.go new file mode 100644 index 00000000..d3baae90 --- /dev/null +++ b/gs/game/audio_video_rtmidi.go @@ -0,0 +1,8 @@ +//go:build windows && rtmidi +// +build windows,rtmidi + +package game + +import ( + _ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv" +) diff --git a/gs/game/game_command_gm.go b/gs/game/game_command_gm.go index 83ba1952..7ed9261b 100644 --- a/gs/game/game_command_gm.go +++ b/gs/game/game_command_gm.go @@ -590,8 +590,6 @@ func (g *GMCmd) GMSetPlayerStaminaInf(userId uint32, open bool) { // 系统级GM指令 -// TODO 不知道为什么0个参数的函数会反射调用失败 - func (g *GMCmd) ChangePlayerCmdPerm(userId uint32, cmdPerm uint8) { player := USER_MANAGER.GetOnlineUser(userId) if player == nil { @@ -639,10 +637,10 @@ func (g *GMCmd) AvPlayAudio(fileDataBase64 string) { logger.Error("file data base64 format error: %v", err) return } - go PlayAudio(fileData) + PlayAudio(fileData) } -func (g *GMCmd) AvStartMidiInputDev(v bool) { +func (g *GMCmd) AvStartMidiInputDev() { err := StartMidiInputDev() if err != nil { logger.Error("start midi input dev error: %v", err) @@ -650,7 +648,7 @@ func (g *GMCmd) AvStartMidiInputDev(v bool) { logger.Info("start midi input dev ok") } -func (g *GMCmd) AvStopMidiInputDev(v bool) { +func (g *GMCmd) AvStopMidiInputDev() { StopMidiInputDev() logger.Info("stop midi input dev ok") } @@ -748,7 +746,7 @@ func (g *GMCmd) SendMsgToPlayer(cmdName string, userId uint32, msgJson string) { GAME.SendMsg(cmdId, userId, 0, msg) } -func (g *GMCmd) StartPubg(v bool) { +func (g *GMCmd) StartPubg() { iPlugin, err := PLUGIN_MANAGER.GetPlugin(&PluginPubg{}) if err != nil { logger.Error("get plugin pubg error: %v", err) @@ -758,7 +756,7 @@ func (g *GMCmd) StartPubg(v bool) { pluginPubg.StartPubg() } -func (g *GMCmd) StopPubg(v bool) { +func (g *GMCmd) StopPubg() { iPlugin, err := PLUGIN_MANAGER.GetPlugin(&PluginPubg{}) if err != nil { logger.Error("get plugin pubg error: %v", err) @@ -774,13 +772,13 @@ func (g *GMCmd) SetPhysicsEngineParam(pathTracing bool) { engine.SetPhysicsEngineParam(pathTracing) } -func (g *GMCmd) ShowAvatarCollider(v bool) { +func (g *GMCmd) ShowAvatarCollider() { world := WORLD_MANAGER.GetAiWorld() engine := world.GetBulletPhysicsEngine() engine.ShowAvatarCollider() } -func (g *GMCmd) AiWorldAoiDebug(v bool) { +func (g *GMCmd) AiWorldAoiDebug() { aiWorld := WORLD_MANAGER.GetAiWorld() if aiWorld == nil { return diff --git a/gs/game/game_tick_manager.go b/gs/game/game_tick_manager.go index 3eafd5d4..764b5616 100644 --- a/gs/game/game_tick_manager.go +++ b/gs/game/game_tick_manager.go @@ -358,12 +358,12 @@ func (t *TickManager) onTick100MilliSecond(now int64) { func (t *TickManager) onTick50MilliSecond(now int64) { // 音乐播放器 - for i := 0; i < len(AUDIO_CHAN); i++ { + for i := 0; i < len(AudioChan); i++ { world := WORLD_MANAGER.GetAiWorld() GAME.SendToWorldA(world, cmd.SceneAudioNotify, 0, &proto.SceneAudioNotify{ Type: 5, SourceUid: world.GetOwner().PlayerId, - Param1: []uint32{1, <-AUDIO_CHAN}, + Param1: []uint32{1, <-AudioChan}, Param2: nil, Param3: nil, }, 0) diff --git a/gs/game/player_common.go b/gs/game/player_common.go index 8d91a78f..f779c289 100644 --- a/gs/game/player_common.go +++ b/gs/game/player_common.go @@ -265,7 +265,11 @@ func (g *Game) GmTalkReq(player *model.Player, payloadMsg pb.Message) { return } funcName := commandText[:beginIndex] - paramList := strings.Split(commandText[beginIndex+1:endIndex], ",") + funcParam := commandText[beginIndex+1 : endIndex] + paramList := make([]string, 0) + if funcParam != "" { + paramList = strings.Split(funcParam, ",") + } commandMessageInput <- &CommandMessage{ GMType: SystemFuncGM, FuncName: funcName,