I used Keras in Python to design a neural network calculating something like a noise-reducing-function. It works pretty good so far, and now I want to use this network to clean the data inside a Unity-Project of mine.
I would not have thought that this could be so difficult. I could only find one python interpreter in the asset store, which does not support external python librarys. IronPython is not an option either, because I need to include the Keras Packages.
I found a KerasSharp Project on GitHub, but there is no documentation on how to load an already trained network, and training it at the beginning is not an option. Furthermore it seems like there is no one working on the project anymore, due to the commit history and unanswered questions. Accessing the script via network APIs is probably not an good option either, due to the latency. I need the calculation for every frame.
So my question is: Is there any way I can load a Keras/Tensorflow model in C# or Unity
OR
Can I somehow access the python script which is calculating the noise-reducing-function using the Keras model?
If your situation allows for you to start the python script after Unity, you can try starting the python script as a subprocess as described here:
http://answers.unity.com/answers/14156/view.html
If you do not require the other process to be running before the Unity
one, you could have your Unity project launch that via Process and
then redirect stdin/out to streams and communicate through these.
Example:
Process otherProcess = new Process ();
otherProcess.StartInfo.FileName = path;
otherProcess.StartInfo.CreateNoWindow = true;
otherProcess.StartInfo.UseShellExecute = false;
otherProcess.StartInfo.RedirectStandardInput = true;
otherProcess.StartInfo.RedirectStandardOutput = true;
// Now communicate via streams
// otherProcess.StandardOutput
// and
// otherProcess.StandardInput
It is also possible that grabbing a running process by name or pid and
then setting the forwarding would work, but I've not tested this and
it does seem rather doubtful.
This setup would require that your python script be able to take in data from standard in, and output its results over standard out.
Related
I developed some somewhat popular Flash games around a decade ago, and recently people have been asking me to port them to Steam. I've been researching how to do this for a few days, but it feels like trying to figure out how to get a GameBoy cartridge working on an iPhone, and googling for answers feels like archaeology since nobody talks about Flash anymore.
Flash can build to an exe projector with the Flash Player bundled, but it stores saved data in fragile cookies, so I need to build a wrapper that can exchange data with it to save data more reliably. I've managed to build something simple in Windows Forms (with which I have only limited experience), but there are two possibilities, each with different issues:
One is that I load in the exe win projector version of the Flash file as a Process:
game = Process.Start ( "MARDEK.exe" );
game.WaitForInputIdle ( );
game.EnableRaisingEvents = true;
game.Exited += ( sender, e ) => { Close ( ); };
This works, and doesn't require the user to have Flash Player installed, but I don't know if it can exchange data both ways with C#.
Another possibility is to embed the swf directly:
swf = new AxShockwaveFlashObjects.AxShockwaveFlash ( );
Controls.Add ( swf );
swf.Size = new Size ( 720, 528 );
swf.Location = new Point ( 10, 10 );
swf.BringToFront ( );
swf.LoadMovie ( 0, "MARDEK.swf" );
I think the swf could then use something called ExternalInterface to communicate back and forth with C#. A huge issue though is that I don't think the Flash Player is being bundled, so the user's expected to have that installed separately. That's not acceptable.
I've found some .dll files for the Flash Player installed on my system, but I don't know if it's possible to bundle them in and have the AxShockwaveFlashObjects use them instead of what the user has installed. That'd probably be the ideal situation.
I know this is an obscure issue - which is why I'm having to ask because Google's telling me so little - but any help would be greatly appreciated!
Yo, I've published both AS2 and AS3 Flash games on Steam. Here's what I can remember right away:
The easy way is to make an AS3 Adobe AIR wrapper that loads a swf game file using SWFLoader (if you want to use AIR's Flashplayer, EBF3 uses this) or HTMLLoader (if you want to use the web Flashplayer, EBF5 uses this) and communicates with it using LocalConnection. This works similarly with both AS2 and AS3 swfs. LocalConnection is pretty reliable unless the user's computer is under a lot of stress - then the connection may be closed - so it may be worth checking/remaking it every once in a while.
I'm not actually sure if you need the web Flashplayer, or if AIR's flashplayer is sufficient. Either way, AIR's Flashplayer removes the LOW and MEDIUM stage quality options, which may be a good reason to use the web one if that's a feature you need.
The latest versions of AIR will not include a web Flashplayer in the package (expecting the user to have it installed). You can either use an older version of AIR (AIR 21 or earlier, requires pirating Flash CC2015) to enable a "captive" Flashplayer, or use this obscure work-around in newer versions:
Install NPAPI flash player from https://get.adobe.com/flashplayer/npapi/
Download the latest AIRSDK from https://adobe.com/devnet/air/air-sdk-download.html then remove/rename these files:
\runtimes\air-captive\win\Adobe AIR\Versions\1.0\Resources\NPSWF32.dll
\runtimes\air-captive\win64\Adobe AIR\Versions\1.0\Resources\NPSWF64.dll
And replace them with:
C:\Windows\SysWOW64\Macromed\Flash\NPSWF32_32_0_0_192.dll (paste it in the above location and rename the file to NPSWF32.dll)
And:
C:\Windows\System32\Macromed\Flash\NPSWF64_32_0_0_192.dll (paste it in the above location and rename the file to NPSWF64.dll)
Basically you're just changing some placeholder files to trick the compiler into including Flashplayer. If you publish your AIR application after changing the files, the user won't need to have Flashplayer installed.
You wouldn't be able to figure this out yourself. I got help from Adobe programmers after my complaints went viral on Twitter. It's poorly documented but once you know what to do, it's not a lot of work.
That's the hard stuff done.
You can use the FRESteamWorks() ANE to easily implement Steam achievements, leaderboards, and other features. ( https://github.com/Ventero/FRESteamWorks )
You'll just need to include that in your wrapper source files. The game swf should tell the wrapper when you want to unlock an achievement etc.
As for save data for games, the lazy option would be to keep using Flash sol files. They seem to be less likely to be deleted if they are made by a desktop app. Otherwise you'd have to send your data though LocalConnection (possibly in several parts, I think the limit is 40kb at a time) to the wrapper, which could then create a text file in whatever format you like. If you make the save files in the installation folder, then Steam cloud can automatically back them up without any extra code needed from you.
Another thing to watch out for: The Steam Overlay will freeze if your swf graphics are not being updated. If you have any moments with no animation, you must hide some animation in there so that the overlay keeps updating correctly.
Get in touch if you want more details!
I'm currently building a program in C# which will call functions in provided python script files.
Some of these script files calls _getframe() in sys, which results in the error:
System.MissingMemberException: 'module' object has no attribute
'_getframe'
(Since IronPython doesn't have _getframe activated by default.)
I have done quite a lot of googling and found out that you can activate it in ipy.exe by providing -X:Frames as a command line option, however this doesn't solve my problem since I'm not directly using ipy.exe to execute the python code.
In this thread they mention rebuilding IronPython from source with the command line options, I downloaded the source files but have no idea how to build it with those options.
Also they mention that the options are in the official installer, I have run the installer exe several times but haven't seen a glimpse of those options there.
When creating the PythonEngine you can pass a dictionary of options; you just need to set the "Frames" and/or "FullFrames" keys in the dictionary to true:
var options = new Dictionary<string, object>();
options["Frames"] = true;
options["FullFrames"] = true;
ScriptEngine engine = Python.CreateEngine(options);
If you don't want FullFrames, just leave it out or set it to false.
A little out of the scope of the question, but meant for anyone else getting this error by invoking a Python script using the ipy.exe interpreter directly.
You can just add the argument -X:FullFrames. So for example invoke the script like
ipy.exe -X:FullFrames script.py
I work for a company that makes application's in C#.
recently we got a customer asking us to look in to rebuilding an application written in PHP.
This application receives GPS data from car mounted boxes and processes that into workable information.
The manufacturer for the GPS device has a PHP class that parses the received information and extracts coordinates. We were looking in to rewriting the PHP class to a C# class so we can use it and adapt it. And here it comes, on the manufacturers website there is a singel line of text that got my skin krawling:
"The encoding format and contents of the transmitted data are subject to constant changes.
This is caused by implementations of additional features by new module firmware versions which makes it virtually impossible to document it and for you to properly decode it yourself."
So i am now looking for a option to use the "constantly changing" PHP class and access it in C#. Some thing link a shell only exposing some function's i need. Except i have no idea how i can do this. Can any one help me find a solution for this.
I know it's a really hacky solution, but if you need a bit of PHP code that you don't want to have to repeatedly port to C# each time, you could try the following approach, although it means that you would need the php command line tool on the target machine.
First step is to have a php script that continously reads data off stdin, decodes it using this special class from the vendor, and writes the result out to stdout. Really simple example:
<?php
include("VendorDecodingClass.php");
while(true)
{
$input = fgets(STDIN); //read off of the stdin stream
//can't remember if this is valid, but somehow check that there is some data
if($input)
{
//pass it off to the vendor decoding class
$output = VendorDecoding::decode($input);
fwrite(STDOUT, $output); //write the results back out
}
//sleep here so you don't suck up CPU like crazy
//(1 second may be a bit long tho, may want usleep)
//Edit: From Tom Haigh, fgets will block, so the sleep isn't necessary
//sleep(1);
}
?>
Anyway, once you have that in place, in your C# application, right at the start, create a new Process to run that script and then save the Process instance somewhere, so you can reference the STDIN and STDOUT at a later point. Example:
ProcessStartInfo procStartInfo = new ProcessStartInfo("php", "yourscript.php");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process(); //store this variable somewhere
proc.StartInfo = procStartInfo;
proc.Start();
Then, when you want to decode your data, you just write to the stdin of the php process you created, and wait for a response on the stdout. Using the stdin/stdout approach is a lot more efficient than creating a new process each time you want to decode some data, because the overhead of creating that process can be noticeable.
proc.StandardInput.WriteLine(somedata); //somedata is whatever you want to decode
//may need to wait here, or perhaps catch an exception on the next line?
String result = proc.StandardOutput.ReadLine();
//now result should contain the result of the decoding process
Disclaimer here, I haven't tested any of this code, but that is the general gist of how I might do it.
Something else I just thought of, you will want some mechanism for terminating that PHP process. It may be OK to use Process.Kill, but if the decoding does any file IO, or anything critical you may want to send an interrupt signal to the php script somehow.
I assume the php script is on your machine and returns usefull data. The first -not very elegant solution- that pops into my mind is the following:
Make sure your machine has the php commandline installed, so that you are able to run the php script from commandline. To execute a commandlinetool from C# see code for that here. The returned data now probably needs to get processed my your C# program.
I have never tried this and do not know anyone that has, but I remember comming across this sometime ago and thought I would throw it out there as a possible option for you.
Phalanger is a compiler project that compiles PHP code to IL, so you can use that then have a managed assembly that you reference from your code directly.
If the format is a regex you can try to put it in an application setting file (not resources, these are compiled WITH the application, you can't change them without recompiling the app).
Application settings are not changeable by the user but you can do that by editing the XML.
Or you can set the settings to user mode and then you can change the format from inside your application code.
Why don't you just launch the PHP script from C#, have it output its results to a file and then use that file as input for your C# program?
Personally, I would setup a PHP web service with a proper and stable API that the C# project can access, implement the manufacturers supplied PHP class in the web service and let it be.
I need to create a sound containing tones of many different frequencies. Is there any way to do this in C#?
The only tone generating methods I've seen so far involve console.beep, which works, but only for pure tones (single frequencies).
The Audiere library makes this extremely easy to do. Here's a nearly complete C# program to generate the DTMF tone for the "1" button:
AudioDevice device = new AudioDevice();
OutputStream tone1a = device.CreateTone(697); // part A of DTMF for "1" button
OutputStream tone1b = device.CreateTone(1209); // part B
tone1a.Volume = 0.25f;
tone1b.Volume = 0.25f;
tone1a.Play();
tone1b.Play();
Thread.Sleep(2000);
// when tone1a stops, you can easily tell that the tone was indeed DTMF
tone1a.Stop();
To use Audiere in C#, the easiest way to get up and running is to use Harald Fielker's C# binding (which he claims works on Mono and VS; I can confirm it works in the both full version of VS2005 and using the separate Express 2008 versions of C# and VC++). You'll need to download the Win32 Audiere DLL, lib, and header (which are all in the same zip) and you'll need to build the C# binding from source using both VC++ and C#.
One of the nice benefits of using Audiere is that the calls are non-blocking. You don't have to wait for tone1a to stop playing before you start tone1b, which is clearly a necessity for playing complex tones. I am not aware of any hard upper limits on how many simultaneous output streams you can use, so it's probably whatever your hardware/OS supports. By the way, Audiere can also play certain audio files (MP3, WAV, AIFF, MOD, S3M, XM, IT by itself; Ogg Vorbis, Flac, Speex with external libraries), not just pure generated tones.
One possible downside is that there is a slightly audible "click" as you start or stop an individual tone; it's not noticeable if you add one tone to an already playing tone. The easiest workaround I've found for that is to slowly ramp the tone's volume up or down when you're turning the tone on or off, respectively. You might have to play around with the ramp speed to get it to sound "just right".
Note that Audiere is LGPL-licensed, and the binding has no license attached to it. You'll have to consult your legal team or try to get a hold of Harald if you want to use his binding in a commercial product; or you could just make your own binding and avoid the hassle.
#Tom: Since there is no specific license attached to Harald's library, I'm not sure what implications would come of hosting it; however, I believe I can at least give you fine detail on exactly how my libaudieresharpglue project is set up.
Using Visual C++ Express 2008, open up bindings/csharp/libaudieresharpglue/vc8.0/libaudieresharpglue.sln. VC++ will automatically convert the solution to a VS9 solution.
In another folder, you should have the Audiere package from Sourceforge. Under your VC++ project properties, go to Configuration Properties > C/C++ > General, and make sure you have path/to/audiere-1.9.4-win32/include in your "Additional Include Directories." Then, in that same window, go to Linker > General and make sure you have /path/to/audiere-1.9.4-win32/lib in your "Additional Library Directories." Then, you should be able to build the project (preferably in Release mode) and this output libaudieresharpglue.dll in your vc8.0/Release folder.
Next, open up Visual C# Express 2008. Open up bindings\csharp\test\vc8.0\AudiereCSharpTest.sln and let it convert the solution. The project should build fine, but then you will get an error when you run it. That's fine; in your csharp/test/vc8.0/bin/Release folder, you need to add both libaudieresharpglue.dll from the VC++ solution and audiere.dll from the package from Sourceforge.
Now, you should be able to build and run AudiereCSharpTest. Note that by default, #define stream_test is not commented out at the top of AudiereTest.cs, and that will reference a file that is not on your hard drive. You can simply comment out that #define and uncomment noise_test or square_test.
That should cover it; if I missed any details, hopefully they are small enough to get by on your own :)
You can always try DirectSound...
I have been looking at NAudio with the view to create a program that emulates feedback whilst playing a backing track. There is a blog post about generating sine waves at specific frequencies, I suspect that this could be adapted to do what you are looking for.
Yes it is possible.
Here is a link to a tutorial on this. but of course this also uses Console.Beep
The MSDN documentation doesn't make it clear if Console.Beep is asynchronous or not. If it is, you can probably fire off as many calls as you need in quick succession and nobody will be the wiser. You'd want to use the version that takes a frequency and a duration, of course.
Essentially, you have to implement your own software synthesizer or find a 3rd party library. See: http://en.wikipedia.org/wiki/Demo_(computer_programming)#Music
Is there a portable, not patent-restricted way to play compressed sound files in C# / .Net? I want to play short "jingle" sounds on various events occuring in the program.
System.Media.SoundPlayer can handle only WAV, but those are typically to big to embed in a downloadable apllication. MP3 is protected with patents, so even if there was a fully managed decoder/player it wouldn't be free to redistribute. The best format available would seem to be OGG Vorbis, but I had no luck getting any C# Vorbis libraries to work (I managed to extract a raw PCM with csvorbis but I don't know how to play it afterwards).
I neither want to distribute any binaries with my application nor depend on P/Invoke, as the project should run at least on Windows and Linux. I'm fine with bundling .Net assemblies as long as they are license-compatible with GPL.
[this question is a follow up to a mailing list discussion on mono-dev mailing list a year ago]
I finally revisited this topic, and, using help from BrokenGlass on writing WAVE header, updated csvorbis. I've added an OggDecodeStream that can be passed to System.Media.SoundPlayer to simply play any (compatible) Ogg Vorbis stream. Example usage:
using (var file = new FileStream(oggFilename, FileMode.Open, FileAccess.Read))
{
var player = new SoundPlayer(new OggDecodeStream(file));
player.PlaySync();
}
'Compatible' in this case means 'it worked when I tried it out'. The decoder is fully managed, works fine on Microsoft .Net - at the moment, there seems to be a regression in Mono's SoundPlayer that causes distortion.
Outdated:
System.Diagnostics.Process.Start("fullPath.mp3");
I am surprised but the method Dinah mentioned actually works. However, I was thinking about playing short "jingle" sounds on various events occurring in the program, I don't want to launch user's media player each time I need to do a 'ping!' sound.
As for the code project link - this is unfortunately only a P/Invoke wrapper.
I neither want to distribute any
binaries with my application nor
depend on P/Invoke, as the project
should run at least on Windows and
Linux. I'm fine with bundling .Net
assemblies as long as they are
license-compatible with GPL.
Unfortunatly its going to be impossible to avoid distributing binaries, or avoid P/Invoke. The .net class libraries use P/Invoke underneath anyway, the managed code has to communicate with the unmanage operating system API at some point, in order to do anything.
Converting the OGG file to PCM should be possible in Managed code, but because there is no Native Support for Audio in .net, you really have 3 options:
Call an external program to play the sound (as suggested earlier)
P/Invoke a C module to play the sound
P/Invoke the OS APIs to play the sound.
(4.) If you're only running this code on windows you could probably just use DirectShow.
P/Invoke can be used in a cross platform way
http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names
Once you have your PCM data (using a OGG C Lib or Managed Code, something like this http://www.robburke.net/mle/mp3sharp/ of course there are licencing issues with MP3), you will need a way to play it, unfortunatly .net does not provide any direct assess to your sound card or methods to play streaming audio. You could convert the ogg files to PCM at startup, and then use System.Media.SoundPlayer, to play the wav files generated. The current method Microsoft suggests uses P/Invoke to access Sound playing API in the OS http://msdn.microsoft.com/en-us/library/ms229685.aspx
A cross platform API to play PCM sound is OpenAL and you should be able to play (PCM) sound using the c# bindings for OpenAL at www.taoframework.com, you will unfortunatly need to copy a number of DLL and .so files with your application in order for it to work when distributed, but this is, as i've explained earlier unavoidable.
Calling something which is located in 'System.Diagnostics' to play a sound looks like a pretty bad idea to me. Here is what that function is meant for:
//
// Summary:
// Starts a process resource by specifying the name of a document or application
// file and associates the resource with a new System.Diagnostics.Process component.
//
// Parameters:
// fileName:
// The name of a document or application file to run in the process.
//
// Returns:
// A new System.Diagnostics.Process component that is associated with the process
// resource, or null, if no process resource is started (for example, if an
// existing process is reused).
//
// Exceptions:
// System.ComponentModel.Win32Exception:
// There was an error in opening the associated file.
//
// System.ObjectDisposedException:
// The process object has already been disposed.
//
// System.IO.FileNotFoundException:
// The PATH environment variable has a string containing quotes.
i think you should have a look a fmod, which is the mother of all audio api
please feel free to dream about http://www.fmod.org/index.php/download#FMODExProgrammersAPI
The XNA Audio APIs work well in .net/c# applications, and work beautifully for this application. Event-based triggering, along with concurent playback of multiple sounds. Exactly what you want. Oh, and compression as well.
Well, it depends on a patent-related laws in a given country, but there is no way to write a mp3 decoder without violating patents, as far as i know. I think the best cross-platform, open source solution for your problem is GStreamer. It has c# bindings, which evolve rapidly. Using and building GStreamer on Windows is not an easy task however. Here is a good starting point. Banshee project uses this approach, but it is not really usable on windows yet (however, there are some almost-working nightly builds). FMOD is also a good alternative. Unfortunately, it is not open source and i find that its API is somehow C-styled.
There is a pure C# vorbis decoder available that is open source:
http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/
Not sure if this is still relevant. Simplest solution would be to use NAudio, which is a managed open source audio API written in C#. Another thing to try would be utilizing ffmpeg, and creating a process to ffplay.exe (the right binaries are under shared builds).
There is no way for you to do this without using something else for your play handling.
Using the System.Diagnostic will launch an external software and I doubt you want that, right? You just want X sound file to play in the background when Y happens in your program, right?
Voted up because it looks like an interesting question. :D