C# audio library with smooth looping? - c#

I'm looking for an audio library that works with .NET that allows for smooth looping. I've tried DirectX AudioVideoPlayback and Mentalis. Both are easy to use, but the looping skips a bit. I'm wondering if that's my fault or theirs. I have sound samples that I know can loop cleanly (WinAmp can do it fine) but I can't get my C# app to do the same. What library could I use, or what could I fix in my app to get it to loop cleanly with the libraries I have?
UPDATE: FMOD has been able to loop my audio, but the problem is that the .net wrapper I have only loads files one way. I can't play a sound effect more than once because they get disposed when playback finishes, and sometimes it hangs whenever a sound is supposed to be played. I know I could just reload the sound to play it again, but I don't want to hit the disk every time a gunshot is fired. Should I just reach into the C++ layer myself and skip the .NET wrappers?

You could try FMOD which is free for non-commercial use.
I would double-check that the sound really loops cleanly - specifically, that the first sample and the last sample are close (or equal), otherwise you'll hear a click. WinAMP could conceivably do some special processing to eliminate the click.
UPDATE: FMOD comes with a whole bunch of samples in C# that show the right way to do stuff. The example called "3d" shows, among other things, a cleanly looping sound. You should be able to rip out the code that handles the looping without utilising the 3D features.

Related

How can I inject/hook into a running process to get information from it?

I'm trying to learn about injecting/hooking running processes.
I have a game running, and in the game you can type /coord to get a print of what your current X, Y coordinates are. Basically I want to just have this done automatically through a program so I was looking into how I might do this with C# but I'm not exactly sure what I would google to start to learn how exactly I might do this, basically I just want a small GUI that will update the X/Y position automatically as I move around.
I'm not exactly sure where to start if anybody can just point me in the right direction that would be great.
If this is too off-topic / broad feel free to close it, sorry for any inconvenience.
This is not really possible with c# only, unless the game is also written in c#, and you can somehow convince the game to load your module.
Otherwise, there will need to be atleast a native stub that starts up the .net runtime.
See this for an example of injecting native code in an application:
http://www.codeproject.com/Articles/4610/Three-Ways-to-Inject-Your-Code-into-Another-Proces
This native code can then bootstrap whatever intermediate language you will use.
On the other hand, reading the process memory can be done from any language that can use the Windows API, using OpenProcess, ReadProcessMemory, CloseHandle.

Intercept and cancel key press events for DirectX game

I'm working on a small tool for a DirectX game and I want to prevent the user from pressing a certain key (F12 in this case) for a certain period.
I could find many options for simulating keypresses but what are the options when it comes to nulling out a keystroke before the game reads it?
The language doesn't really matter, although I would prefer a C# or C++ solution, or just a nudge in the right direction :)
Thanks in advance!
The good news is, I've done this before so I can say that it is possible and it does work.
The bad news is that it's not simple. It requires a lot of complicated code, and will likely take a long time to implement, but I'll explain how you can do it.
Applications like DirectX games usually register for raw input.
Since you want to stop a keyboard event from reaching the application, you need a way to insert your code between the raw input and the game so you can check the raw input and decide whether to allow it to be passed to the game:
So you want to change the flow from:
Raw Input --> Game
to
Raw Input --> Your Code --> Game
Without having access to the source code of the game, you have to find a way to insert your code.
When there is keyboard input available, the game will call the WinAPI function GetRawInputData, which will tell it about the keyboard event. Ideally, what we want is when the game calls this function, it actually calls our code instead of the WinAPI function. Then we can decide what to tell the game about the keyboard event, we could tell it anything we want (e.g. ignore F12). Sounds great right? Here's where it gets interesting...
We can take advantage of how windows loads executables into memory. Typically, a program uses (or 'imports') calls to functions in other DLLs (such as GetRawInputData, in User32.dll). When the program gets loaded into memory, Windows will fill in a table (the Import Address Table (IAT)) with pointers to the executable code in the appropriate DLLs. This means that when the program calls the function, it gets directed to the executable code in User32.dll in memory to run it.
Wouldn't it be great if we could write/patch the address of one of our functions into that table, so that when the game calls GetRawInputData, it actually gets directed to our function for us to process? Well we can! It's called Import Address Table Patching.
There's a pretty good article on it here with some working code in C++. You should first read it to understand in more detail how it works, then you can modify it to support your needs. It will work, but I know it's probably more work (much more work) than you would have been hoping for, but essentially you're hacking the application which is never easy to do.
It's worth doing, even just to gain a better understanding of Windows behind the scenes.
Good luck!
EDIT
As Simon said, Windows Hooks is a much simpler way to do it if the game isn't using raw input. DirectX Games tend to be a special case that don't really work too well with standard Hooks as they use special methods to get the input from the user. By all means give it a go though, it will be a lot easier if it works.

WPF animation (scaling/translating multiple images at the same time)

I am working on a project in C# using WPF to attempt to make a program much like http://www.scaleofuniverse.com/.
I know it can easily be done in flash but I want to integrate the controls into other platforms (ie the new Leap motion and Kinect). I know I could rig it so that the kinect emulates key presses onto a flash game but i would really like to figure out making this in C#. I completed a few basic tutorials on basic animation and storyboarding but i've found nothing that indicates the project i want to complete can even be done! Am i biting off more than i can chew or am i over-complicating the program somewhere? I am only in the design stage so i do not have any source code for review (still completing tutorials). I really want to know if this project is even plausible before I continue working on it.
It definitely can be done in WPF. Here is an example of a custom zoom control for WPF http://www.ab4d.com/ZoomPanel.aspx.

C# WinForms application to display waveforms of playback and recorded sound

I wish to write a C# WinForms application that can play a WAV file. While playing the file, it shows a waveform (similar to an oscilloscope).
At the same time, a user can record sound via the microphone, attempting to follow the original sound played (like a karaoke). The program displays the waveform of the recorded sound real-time, so comparisons can be seen from the waveform display of the original
wave file and the recorded one by the user. The comparisons will be done as in the difference in time (the delay) of the original and recorded sound. The waveform displays don't have to be very advanced (there is no need for cut, copy or paste); just being able to see it with a timeline would suffice.
I hope this is clear enough. Please do not hesitate to ask for more clarification if it's not clear. Thank you very much.
You can do what you want with C#, but it isn't going to work like you think. There is effectively no relationship at all between how a recording looks in an oscilloscope-type display and how that recording sounds to a human ear. So, for example, if I showed you two WAV files displayed in an oscilloscope display and told you that one recording was of a tuba playing and the other was of a person speaking a sentence, you would have no idea which was which just from looking at them.
If you want to compare a user's sounds to a pre-recorded WAV, you have to get more sophisticated and do FFT analysis of both and compare the frequency spectra, but even that won't really work for what you're trying to do.
Update: after some thought, I don't think I fully agree with my above statements. What you want to do might sort of work if what you want to do is to use the oscilloscope-type effect to compare the pitch (or frequency) of the WAV and the person's voice. If you tuned the oscilloscope to show a relatively small number of wavelengths at a time (like 20, maybe), the user would be able to quickly see the effect of raising or lowering the pitch of their voice.
I have a small sample C# app that I wrote about 2 years ago that does something kind of like this, only it displays an FFT-produced spectrograph instead of an oscilloscope (the difference is basically that a spectrograph shows frequency-domain information while an oscilloscope shows time-domain information). It's realtime, so you can talk/sing/whatever into a microphone and watch the spectrograph change dynamically.
I can dig this out and post the code here if you like. Or if you want the fun of doing it all yourself, I can post some links to the code resources you'd need.
The NAudio library has plenty of functionality that will (possibly) give you what you need. I've used it in the past for some simple operations, but it is much more powerful than I've had need to use.
#ZombieSheep
Naudio is indeed useful, but it has limitations. For example, there is not much control over the waveform display, it cannot be cleared and redrawn again. Besides, if it gets too long its impossible to scroll back to see the waveform in the front part. One more thing is that it only works with playing the sound but does not work with recording the sound.
Thank you.

video call in c#

I want to make a similar application to Skype, and the main problem is working with video and audio. The first problem is how to get a bytes array of the video (to be specific, I need to get bytes which represent the video, so that I can send them over the internet), and same with audio. The second problem is to play bytes that come from the other computer.
I've been thinking to do that in WPF. I'm new in WPF (I have practiced a little bit, and made couple of programs among which is a basic chat program). I'm doing this for practice, and I want to code by myself as much as I can, server, client, transmision of data, and so on...
I've been searching over the internet, and only one solution seems to me to be good, or better to say feasible, is to use DirectShow.
Just to add, I know that camera and microphone is supported in Silverlight, and I've tried that (actually, I've tried to host an HTML page with silverlight project in WPF project in which were webbrowser control, and I've succeeded to show video from my webcam), but I don't know how to get bytes which represent video.
Is that possible to do with WPF or silverlight?
I'll be very grateful for suggesting any solution, advice, or useful links.
Using DirectShow filter graphs, you'll have a direct access to image and audio buffers from input devices (such as cameras and microphones) as bytes array, sample by sample. You'll be able to directly manipulate the data, to chose a coding or compression format (using specific filters), and to control the data rate and synchronization.
However :
if you've entirely new to this environment, it will be hard. Also, I know it works nicely with C++, but I've never coded any Directshow application in C#. (You may want to look this way : CodeProject Tutorials, MSDN DirectShow topics, and tests using graphedit)
streaming media accross a network and receiving it with Directshow is not trivial and can be quite a pain. Network renderers and network source filters are available all around, but are always difficult to use in my opinion. And depending on your video format (H264, MPEG, MJPEG...) and network protocol (RTSP, plain old simple UDP...) choices, you might end up having to write your own stream/source filters, which is hard and time consuming.
Nevertheless, it IS feasible, and if your main objective is practice with coding, then why not !
(Never used WPF, maybe it's actually way simpler !)
I can't speak to WPF or Silverlight, but I've done this in DirectShow, and it's a pain in the ass.
If you want to use .NET, there's an open source wrapper called DirectShow.NET, that helps alot, and it's still a pain in the ass.
Microsoft did a good job with DirectShow and the whole Filter-Graph thing, but then they sort of dropped the ball a while ago and haven't updated it in years.
I'd recommend looking for a different technology(although it probably sits on DirectShow), and I'd be interested to hear what you find.
To all who are interested in this subject,
After spending hours and hours searching the internet, i managed to find a solution that should work. With Silverlight i take captures, resizing them to 160x120 (or less), and than convert them with imagetools. One thread that is responsible for taking pictures, starts capture, and when it is finished (capturing is asynchronous, so you need semaphores to use) it sleeps for 200ms; thats almost equivalent to 5 frames per second. I'm doing all of this because i have slow upload bandwidth, about 16 kilobytes per second, so i have to compress one frame as much as i can. Result is low detailed picture, but if u use 100x100 rectangle for viewing it, it isn't too bad. I haven't tried it with the internet yet, but, as i have said, it should work. I've also tried using compress methods, to compress picture a little more, if it is possible, but i don't know how to use that class (something is not working well), so left that for another time. Now i just want to make it work, and latter i'll try to make better performance.
Oh, one more thing, I also have to solve problem with audio transmission, and that needs a lot of work.
So, hear latter.

Categories

Resources