forked from Ryochan7/Touchmote
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added speaker output to Wiimote handler.
- Loading branch information
Showing
15 changed files
with
218 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace WiiTUIO.DeviceUtils | ||
{ | ||
internal class AudioUtil | ||
{ | ||
private static readonly string[] audioExtensions = { ".wav", ".mp3", ".aac", ".ogg", ".flac" }; | ||
|
||
public static bool IsValid(string fileName) | ||
{ | ||
string baseFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", fileName); | ||
|
||
foreach (string extension in audioExtensions) | ||
{ | ||
string filePath = baseFilePath + extension; | ||
|
||
if (File.Exists(filePath)) | ||
{ | ||
if (extension == ".wav" && IsValidFormat(filePath)) | ||
{ | ||
return true; | ||
} | ||
|
||
return ConvertToYamahaADPCM(baseFilePath, extension); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool IsValidFormat(string filePath) | ||
{ | ||
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) using (BinaryReader reader = new BinaryReader(fs)) | ||
{ | ||
byte[] riff = reader.ReadBytes(4); | ||
if (System.Text.Encoding.ASCII.GetString(riff) != "RIFF") | ||
return false; | ||
reader.ReadInt32(); | ||
|
||
byte[] wave = reader.ReadBytes(4); | ||
if (System.Text.Encoding.ASCII.GetString(wave) != "WAVE") | ||
return false; | ||
|
||
byte[] fmt = reader.ReadBytes(4); | ||
if (System.Text.Encoding.ASCII.GetString(fmt) != "fmt ") | ||
return false; | ||
reader.ReadInt32(); | ||
|
||
short formatCode = reader.ReadInt16(); | ||
if (formatCode != 0x0020) | ||
return false; | ||
|
||
short channels = reader.ReadInt16(); | ||
if (channels != 1) | ||
return false; | ||
|
||
int sampleRate = reader.ReadInt32(); | ||
if (sampleRate != 3000) | ||
return false; | ||
reader.ReadBytes(6); | ||
|
||
short bitsPerSample = reader.ReadInt16(); | ||
|
||
return bitsPerSample == 4; | ||
} | ||
} | ||
|
||
private static bool ConvertToYamahaADPCM(string baseFilePath, string extension) // Use ffmpeg to convert to valid audio file | ||
{ | ||
string filePath = baseFilePath + extension; | ||
string outputPath = baseFilePath + ".wav"; | ||
if (extension == ".wav") | ||
{ | ||
File.Move(filePath, filePath + ".bak"); | ||
filePath += ".bak"; | ||
} | ||
|
||
if (!Launcher.Launch(null, "ffmpeg", $"-i \"{filePath}\" -ar 3000 -ac 1 -c:a adpcm_yamaha \"{outputPath}\" -hide_banner", null)) | ||
{ | ||
if (extension == ".wav") File.Move(filePath, baseFilePath + extension); | ||
return false; | ||
} | ||
|
||
return File.Exists(outputPath) && new FileInfo(outputPath).Length > 0; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.
3237af0
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.
OMG! Great one!
Been doing some quick test, didnt have much luck
Tried with some .wavs on
C:\Program Files\Touchmote\Resources/sound1.wav
C:\Program Files\Touchmote\Resources/sound2.wav
Still couldnt get any sound. Im trying on a non TR, which shouldnt get proper sound, but at least some garbage sound should play. Will try later on a TR one.
3237af0
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.
The audio file should be encoded as Yamaha ADPCM mono at 3000hz, if not you should have the ffmpeg binary either in the root directory or accessible within your PATH.
3237af0
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.
After some debugging, it says cant find the file. Will try other formats or names.
BTW, as a cherry on the top will be awesome to have a settings.json item for set speaker volume.
Edit. Ops. Didnt saw your message. completely forgot about ffmpeg! Will try!
3237af0
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.
Perfect garbage sound on a non TR after adding ffmpeg! Cant wait to try it on a TR one!
3237af0
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.
As for future ideas, on my rudimentary implementation I couldnt find a way to stop a sound on button release, which will be nice for machine gun style sounds,
Since I cannot find a way to do that, I implemented an empty 1 sec audio file which will play on button up, that way will interrupt the machine gun sound on button release. It could be called stop1, stop2 on resources folder and only be used when want a sound to be interrumpted on button release. If they are not present, just play it normal.
Loop/repeat play while holding button down wont be difficult to implement too.
3237af0
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.
Well, for some reason, TR ones doesnt work with latest commits.
It says paired but leds keep blinking and wont connect to touchmote. Non TR pairs fine. Im using new wiimotelib from this build.
Debug stay on a loop of this while pairing:
Scanning...
wiimoteConnectorTimer_Elapsed
Could not establish connection to a Wiimote: No Wiimotes found in HID device list.
Found:Nintendo RVL-CNT-01-TR
BluetoothGetRadioInfo: ok
Radio 0: PCNAME 00:1a:7d:da:xx:xx
Scanning...
Found:Nintendo RVL-CNT-01-TR
BluetoothGetRadioInfo: ok
Radio 0: PCNAME 00:1a:7d:da:xx:xx
Scanning...
Dont get any more info from debugging.
3237af0
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.
Would you happen to know what's the last commit they worked with?
I don't have TR wiimotes so I don't really have a way to test it myself.
3237af0
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.
I think is related to this commit and to wiimotelib.
If I use the old wiimote lib with this commit, both TR and non TR won´t connect. Both say paired but leds will keep blinking. With this commit and new wiimotelib non TR will connect and TR will keep blinking.
And this one is interesting, if I replace the new wiimotelib to a build previous to this commit where both pair fine, TR wont pair anymore. So it´s related to new wiimotelib. Maybe touchmote used a modified wiimotelib 1.7 with some stuff for the TR ones pair?
3237af0
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.
I think I found the problem
https://github.com/Suegrini/WiimoteLib/blob/master/WiimoteCS/WiimoteLib/Wiimote.cs
Is missing private const int PID = 0x0330;, only private const int PID = 0x0306; is available there.
Changed to 0330 and TR paired fine.
But also read this, not sure about sensors
BrianPeek/WiimoteLib#2
Bad part is sound is all distorted like non TR, probably is threating the TR like a non TR. Also the changes made to get proper sound on TR ones was made probably after latest 1.7 wiimotelib release. I guess segfault has this code on his wiimotelib. But has other mods too which doesn't seem good to us.
This is the commit that fixed speaker audio for TR ones on dolphin dolphin-emu/dolphin@8acf8cf
And here is some interesting discussion about audio format/hz/coding needed for TR ones https://bugs.dolphin-emu.org/issues/5977
And last but not least, this fork has some extra fixes but I think just for balance board, in case you want to merge them https://github.com/lshachar/WiimoteLib
3237af0
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.
Now I remember Ryochan7 released a wiimotelib net8 some months ago.
https://www.mediafire.com/file/v6p0lvg87ab6m7r/WiimoteLibNet8.zip/file
I can see private const int PID = 0x0330; is there but its called private const int PID_TR = 0x0330; and maybe other stuff. Maybe we can use this lib as a base. It should be based on touchmote one I guess. Most files seems newer that brianpeek ones.
3237af0
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.
Hi. Thanks for all the fixes! We are getting closer, still touchmote doesnt like that much playing audio 🗡
I made a comparisson touchmote vs wiimoteaudioplayer on a TR wiimote.
https://www.mediafire.com/file/hh2jvg98fvfbluc/Touchmote+vs+WiimoteAudioPlayer.aac/file
Besides distortion, touchmote also seems to play just a few secs of the song, around 2 o 3 seconds.
3237af0
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.
Hey, thanks for all the help debugging this. Like I said before, I don't actually have TR wiimotes to test this. Could you try testing it with the latest commit to https://github.com/Suegrini/WiimoteLib?
3237af0
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.
Hi! Tried but no improvements. No need to hurry trying to track down this. Im pretty sure the fix is around that dolphin commit dolphin-emu/dolphin@8acf8cf (that fixed audio for TR on dolphin) and segfault wiimotelib code https://github.com/trigger-segfault/WiimoteLib.Net/blob/master/WiimoteLib/Wiimote.cs Seems pretty different. There is no mention to vid pid for wiimotes, part of the code should have moved to other files.
I´ll give it a read a couple of times again. But dont think is about timers, probably is about packets or writing or buffering.
Still the actual progress is amazing. We will figure this out :)
This seems interesting too:
" Genuine Wii Remotes seem to operate at 100hz until "sniff" mode is enabled.
This is observable from the rate of input reports and activity on the i2c extension bus.
The 6khz audio data produces 150 speaker data packets per second.
Remotes tend to not process many of these causing their decoder to desynchronize from the games' encoder.
"TR" remotes seem to buffer the reports better. They still perform poorly without sniff mode, just not as bad as non-TR.
The Wii sends "HCI_Write_Link_Policy_Settings" (0x000D) and "HCI_Sniff_Mode" (0x0003).
Both the min and max intervals are set to 5ms.
Remotes then operate at 200hz and handle the high rate of speaker data. "
`
3237af0
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.
Hi I read the code several times but Im a little lost here.
Just asked IA what differences can see on many files shared and I got this
https://www.mediafire.com/file/k6mfozcjcr258b9/Wiimote+CS+Differences.zip/file
Not sure if will help, Probably not. But mention something about buffer and packets size. Maybe we can create another wiimotelib branch for testing purposes.
IA suggested this one as a possible fix, but it wont compile, probably many sintaxis errors among other things.
https://www.mediafire.com/file/11q61v14alnm42p/Wiimote_Sueg_Improved_Fixed.cs/file