WebCam: Switch on and of or or keep on - c#

Good day,
I wasn't sure if I should post this on the software or hardware stack; I apologize in advance if this is an invalid question.
I wrote a little application that I am using to make time lapse videos - currently it only takes the pictures using a webcam. I know there are already a few available for download, but none of them did 100% what I wanted, and some of them were a little buggy, so I decided I'd rather create my own.
The interval at which the pictures are taken can be configured anywhere from 5 seconds up. Version 1.x would activate the camera and keep it on while in "Time Lapse Mode" and save images to disc at the specified intervals. This approach proved to be very memory intensive - understandably, in retrospect.
I decided to start from scratch - Version 2.x. This version would keep the camera off and only switch it on when it needed to take a picture, and switch it off again. This approach proved much more efficient. The reason for the minimum limit of 5 second intervals is because the camera takes about 1 second to switch on and then roughly the same amount of time to switch off. Perhaps in the future I could change it to keep the camera on when the interval < 5. For now, however, for what I actually want to use it, this will do perfectly.
When I was little we, as children, were told that switching an incandescent bulb on and off and on and off is not good for the bulb - according to a colleague of mine, reliable in that field, this is true.
This got me thinking. Could it be harmful to my webcam if I switch it on and off at, say, 10 second intervals for, let's say, a day or two? And how would switching on and off compare to keeping the device on for a few days? I don't understand what happens on a hardware level so I can't say.
I suppose I have a couple of options:
Switch the application on and off as required to take the pictures. This could result in the camera being switched a few thousand times a day.
Keep the camera on. This could mean the camera might be active for very long periods of time. What if I want to create a time lapse video over a month? Or even a year? Not to mention the memory problem.
Switch between the two modes. When interval < 2 minutes ? keep on : switch. This seems like the best of both worlds but now I'm faced with the memory problem when interval < 2 minutes
Thank you in advance for any and all comments and suggestions - much appreciated.
Kind regards,
me.

Could it be harmful to my webcam if I switch it on and off at, say, 10 second intervals for, let's say, a day or two?
Switching the camera on and off will have no affect on it's lifespan.
How about keeping it on for long periods of time?
Well that really depends on the camera but for something as low powered as a webcam you should be able to run it for many many years before it begins to fail.
Not sure how you are getting frames from your camera but it should not be extremely memory intensive. Using AForge.NET you can pretty simply grab frames from you camera. Tutorial If you could post your code I could better see how to point you in the direction of optimizing it.

Related

c# wpf memory leak

I've recently made a desktop application that communicates with a device at my job.
The general idea of the app is to give the device (oven) a "set temperature" command and after every 10 seconds check the current temperature and display it on a graph using livecharts.
This application is required to run multiple days at a time and I seem to be having a memory leak problem, I think.
What I experience is the application not responding for a while, then it becomes responsive and adds 1 single "log" effect as in LogTemp function every around 1-2 minutes. It should be once every 10 seconds.
Edit: Just read the lines before this edit and I think I was not too clear. It works as it is supposed to work the first few hours. Noticed the performance took a hit only after 24 hours.
After 24 hours of running I came back to find it is using over 800 MB of RAM and it kept growing by the second.
I suspect it MAY have something to do with livecharts but I am not sure by any means. (it ends up with 8640 points of data after 24 hours)
I have no issue disclosing my code and have even minimized the amount of code needed to be shown to around 200 lines in total which are split to a few different functions, but if anyone heard of such an issue with livecharts and/or can suggest a different type of graph library I'd be more than happy to swap it out.
Actually, here's the code, lmao:
https://pastebin.com/YBn5CuD6
Another thing I thought of, could it be that I am adding too many rows to the ListView? We're talking about.. 8640 rows in 24 hours. Might that be something to do with it?
Sorry for the long post, thank you in advance.
For anyone interested, I lowered the sample rate which is responsible for how many points are on the LiveCharts chart and it goes very smooth right now, even after 3 days of running.
(RAM usage was also around 90 MB, which is to be expected from my application)
I believe that was the issue, so I'll mark this as an answer for the next person googling LiveCharts memory-leak.

Keeping track of elapsed time C#/Unity

I'm creating a rhythm game in C# and Unity, and I haven't been able to find a direct correlation between the problems some of my players are having and their hardware but some of them are having problems with the gameplay gradually desyncing from the music over time. It usually takes a couple minutes before it's noticeable, but it definitely happens and ruins the feel of the game.
I'm keeping track of the elapsed time by using a double and adding Time.deltaTime (the time between frames) to it, and I'm assuming that there's just very small precision errors associated with doing that and that's what's causing the gradual desyncs.
Is there a better way to do this that's more accurate? I'm thinking that getting the time that the gameplay started, and then on each frame subtract that from the current time might make it more accurate? I've tried syncing the gameplay time to the audio time (Bass.NET provides a way to get song position in seconds), but while that works for me and quite a few others it ends up making others with decent hardware stutter (my game generally runs just fine on even 8-10 year old PCs).
Depending on your needs, you can use Time.timeSinceLevelLoad, Time.realtimeSinceStartup (although you are advised to use Time.time instead), or most likely Time.time. If you use Time.timeScale to pause your game, you can use Time.unscaledTime.
The difference between Time.realtimeSinceStartup and Time.time is that Time.realtimeSinceStartup will keep on counting even if you pause your game with Time.timeScale. Assuming that users might pause your game, which may or may not stop your audio from playing, would help you strategize whether you are going to use which property. You may want to set Application.runInBackground to true to get most reliable results, depending on your game logic.

Reduce or predict sound latency

I know that a certain delay between the “play-order” and the actual start of the playback of a sound is inevitable.
However, for my current project, I must be able to start a sound-playback at a certain moment in time. This moment is known, so the solution to the problem is ether to reduce the delay-time as much as possible or to somehow predict the latency and start the sound somewhat earlier (depending on the predicted latency).
I describe the problem in detail here:
https://naudio.codeplex.com/discussions/662236
My current solution is to use NAudio to play a sound and simultaneously observe the sound-output-volume. This way I can measure the latency and use it to time the “play-order” for the following sounds.
This way I get decent results (about 30 ms deviation from the supposed play-time), but I wanted to ask if you guys have better suggestions.
Best regards and many thanks

Extremely low FPS, what profiling application should I use to find performance issues?

I'm creating an XNA game and am getting an unexpected result, extremely low FPS (About 2-12 fps). What program should I use to test performance and track down what is slowing it down?
Have you tried using SlimTune?
You could also use StopWatch to measure sections manually.
Okay, let me bring my personal experience with game development in XNA.
The first thing you need to do is go into Debug -> Start Performance Analysis. This profiles your CPU activity and see's what threads are in use and what is doing the most processing.
You also need to factor in a couple of more things:
-You are probably running in debug mode, this means that some of your CPU is being dedicated to VS and to check for exceptions and what not.
-Your code might be inefficient. I recommend trying to limit the amount of Lists, Arrays, ADT's, and objects created during run time, because that slows it down a lot. Last time I checked the Game Loop ran 60 times a second so that imagine what a strain it would be to allocate a new List, then garbage collect it, 60 times a second. It starts adding up.
-I don't know how advanced you are, but read up on parallel threading, or multitasking.
An example would to have your physics engine 1 frames behind your graphics update.
EDIT: I realized you found your mistake but I hope this post can help others.

How can I compare two captures to see which one is louder?

Given two byte arrays of data captured from a microphone, how can I determine which one has more spikes in noise? I would assume there is an algorithm I can apply to the data, but I have no idea where to start.
Getting down to it, I need to be able to determine when a baby is crying vs ambient noise in the room.
If it helps, I am using the Microsoft.Xna.Framework.Audio.Microphone class to capture the sound.
you can convert each sample (normalised to a range 1.0 to -1.0) into a decibel rating by applying the formula
dB = 20 * log-base-10 (sample-value)
To be honest, so long as you don't mind the occasional false positive, and your microphone is set up OK, you should have no problem telling the difference between a baby crying and ambient background noise, without going through the hassle of doing an FFT.
I'd recommend you having a look at the source code for a noise gate, which does pretty much what you are after, with configurable attack times & thresholds.
First use a Fast Fourier Transform to transform the signal into the frequency domain.
Then check if the signal in the typical "cry-frequencies" is significantly higher than the other amplitudes.
The preprocessor of the speex codec supports noise vs signal detection, but I don't know if you can get it to work with XNA.
Or if you really want some kind of loudness calculate the sum of squares of the amplitudes from the frequencies you're interested in (for example 50-20000Hz) and if the average of that over the last 30 seconds is significantly higher than the average over the last 10 minutes or exceeds a certain absolute threshold sound the alarm.
Louder at what point? The signal's average amplitude will tell you which one is louder on average, but that is kind of a dumb, brute force way to go about it. It may work for you in practice though.
Getting down to it, I need to be able to determine when a baby is crying vs ambient noise in the room.
Ok, so, I'm just throwing out ideas here; I am by no means an expert on audio processing.
If you know your input, i.e., a baby crying (relatively loud with a high pitch) versus ambient noise (relatively quiet), you should be able to analyze the signal in terms of pitch (frequency) and amplitude (loudness). Of course, if during he recording someone drops some pots and pans onto the kitchen floor, that will be tough to discern.
As a first pass I would simply traverse the signal, maintaining a standard deviation of pitch and amplitude throughout, and then set a flag when those deviations jump beyond some threshold that you will have to define. When they come back down you may be able to safely assume that you captured the baby's cry.
Again, just throwing you an idea here. You will have to see how it works in practice with actual data.
I agree with #Ed Swangren, it will take a lot of playing with samples of data for a lot of sources. To me, it sounds like the trick will be to limit or hopefully eliminate false positives. My experience with babies is they are much louder crying than the environment. so, keeping track of the average measurements (freq/amp/??) of the normal environment and then classifying how well the changes match the characteristics of a crying baby which changes from kid to kid, so you'll probably want a system that 'learns'. Best of luck.
update: you might find this library useful http://naudio.codeplex.com/

Categories