Using NAudio, How do I get the amplitude and rhythm of an MP3 file? - naudio

The wife asked for a device to make the xmas lights 'rock' with the best of music. I am going to use an Arduino micro-controller to control relays hooked up to the lights, sending down 6 signals from C# winforms to turn them off and on. I want to use NAduio to separate the amplitude and rhythm to send the six signals. For a specific range of hertz like an equalizer with six bars for the six signals, then the timing from the rhythm. I have seen the WPF demo, and the waveform seems like the answer. I want to know how to get those values real time while the song is playing.
I'm thinking ...
1. Create a simple mp3 player and load all my songs.
2. Start the songs playing.
3. Sample the current dynamics of the song and put that into an integer that I can send to which channel on the Arduino micro-controller via usb.
I'm not sure how to capture real time the current sound information and give integer values for that moment. I can read the e.MaxSampleValues[0] values real time while the song is playing, but I want to be able to distinguish what frequency range is active at that moment.
Any help or direction would be appreciated for this interesting project.
Thank you

Sounds like a fun signal processing project.
Using the NAudio.Wave.WasapiLoopbackCapture object you can get the audio data being produced from the sound card on the local computer. This lets you skip the 'create an MP3 player' step, although at the cost of a slight delay between sound and lights. To get better synchronization you can do the MP3 decoding and pre-calculate the beat patterns and output states during playback. This will let you adjust the delay between sending the outputs and playing the audio block those outputs were generated from, getting near perfect synchronization between lights and music.
Once you have the samples, the next step is to use an FFT to find the frequency components. Fortunately NAudio includes a class to help with this: NAudio.Dsp.FastFourierTransform. (Thank you Mark!) Take the output of the FFT() function and sum out the frequency ranges you want for each controlled light.
The next step is Beat Detection. There's an interesting article on this here. The main difference is that instead of doing energy detection on a stream of sample blocks you'll be using the data from your spectral analysis stage to feed the beat detection algorithm. Those ranges you summed become inputs into individual beat detection processors, giving you one output for each frequency range you defined. You might want to add individual scaling/threshold factors for each frequency group, with some sort of on-screen controls to adjust these for best effect.
At the end of the process you will have a stream of sample blocks, each with a set of output flags. Push the flags out to your Arduino and queue the samples to play, with a delay on either of those operations to achieve your synchronization.

Related

Stm32 bldc driving

As explained here:
http://www.edn.com/design/sensors/4407580/Brushless-DC-Motors-Part-II--Control-Principles
, switching the motor windings should occur when the back-emf voltage across the 1/2 VDCC value. How to effectively perform that in stm32f4 which don't has embedded comparator module?
It seems the only way is using analog watchdog with selecting next single waited channel at every moment when interrupt happens?
And how to be if I want drive 4 bldc from single stm32 chip?
There are few ways you can achieve this. The most popular way with STM32's are sensing the floating phase. The technique is little different to what your link is suggesting, nevertheless there are plenty of example codes to get this going.
Here is a nice explanation of ST's patented 3 resistor BLDLC position sensing method (and some other techniques).
A nice starting point would be this manual.
STM32 supports for two motor control timers (TIM1 and TIM8). You can use them to drive 2 BLDC motors. Nonetheless, it would not limit you to use other timers in combination in order to drive more BLDCs but would demand some additional programming complexity.

Plot a graph of Time vs RSSI for a 433Mhz RF ASK Receiver

Hi Im using the following RF module
http://www.apogeekits.com/rf_receiver_module_rx433.htm
on an embedded board with the PIC16F628A. Sadly, I realized that the signal strength was in analog form and couldn't get any ideas to get the RSSI reading off the pin because well my PIC is digital DUH!.
My basic idea was
To get the RSSI value from my Receiver
Send it to the PIC
Link the PIC to a PC via RS232
Plot a graph of time vs RSSI of the receiver (so I can make out how close my TX is to my RX)
I thought it was bloody brilliant at first but ive hit a dead end here. Any ideas on getting the RSSI data to my PC from this receiver would be nice.
Thanks in Advance
You can get a PIC that has an integrated ADC for sampling the analog signal. Or, you can use an external ADC chip to do the conversion. You would connect that to your PIC using SPI or I2C.
The simplest thing to do is obviously to use a more appropriate microcontroller - one with an ADC! There are many (most), including PICs (though that wouldn't be my first choice).
Attaching an external SPI or I2C ADC might be a bit tedious since having no SPI or I2C on your part, you'd have to bit-bash it. If you do that, use an SPI part - its simpler. Your sample rate will suffer and may end-up being a bit jittery if you are not careful.
Another solution is to use a voltage controlled PWM, then use the timer input capture to time the pulse width. That will give you good regularity and potentially good resolution. You can get a chip (example) to do that, or grow your own. That last option requires a triangle wave input as well as the measured (control) voltage, but on the same site...
In a similar vein, you could use a low frequency VCO (example) and use the output to clock one of the timers, then using a second timer periodically sampling the first and reset it. The count will relate to the voltage, though not necessarily a linear relationship, linearisation could be none on the PIC or at the receiving PC - I'd go for the latter - your micro will suck at arithmetic (performance wise) - even integer arithmetic, especially if it involves division.

Detecting heartbeats signals with "Digital heart beat rate sensor (IC)" - iOS

I just bought Digital heart beat rate sensor:
http://www.dealextreme.com/p/digital-heart-beat-rate-sensor-3-5mm-data-port-16009
And I'm looking how I can make application for iOS to work with.
Sensor has 3.5mm jack and I can detect signal with audio framework on iOS.
Can you give me some guidelines how to start with detecting these signals into heart beat rates?
That sensor looks rather like one I have here in my junk box. If so, it generates a voltage signal which depends on the pressure exerted on it by the skin against which it is pressed. If there is a strong pulse at the point of pressure, I see a signal on an oscilloscope which has a component at the pulse rate: so it is at a frequency of around 1-2Hz.
This is WAY below the audio range, and in most audio interfaces would be filtered out before it ever got to the audio in ADC. I don't have a handy iPhone to check this on, but it would be bad design if the audio input did let such frequencies through. And Mr Jobs (R.I.P.) did not approve of bad design!
There is also a lot of interference at other frequencies: mains hum (50Hz here), and at lower frequencies spurious signals from muscle twitches.
To make this work, you would need some sort of signal conditioning. If it was up to me, I would use a high input impedance amplifier, with about a 0.1Hz - 10Hz passband, followed by a voltage to frequency converter. That would give me a tone, which i could set in the audio band, whose frequency varied up & down as the pressure on the sensor changes. That would let me use fairly simple frequency detection software to recover the pressure waveform, which could then be processed using autocorrelation or similar techniques to recover the heartbeat frequency. A DTMF decoder is not the right tool, though.
I did find when I played about with the senor that it was very touchy, responding to almost everything going, and it wouldn't be easy to pick out the heartbeat. Your sensor may be different, though.

Objective-C play sound

I know how to play mp3 files and whatnot in Xcode iOS. But how do I play a certain frequency, like if I just wanted to emit a C# note for 25 seconds; how might I do that? (The synth isn't as important to me as just the pitch of the note.)
You need to generate the PCM audio waveform that corresponds to the note you want to play and store that into a sample buffer in memory. Then you send that buffer to the audio hardware.
Here is a tutorial on generating waveforms of several types. The article goes into some details on the many aspects to a note you need to consider, including the frequency, volume, waveform shape, sampling rate, etc. The article comes with Flash source code, I think you should have no problem taking the concepts and adapting them to iOS.
If you also need a library that you can use to play the generated buffers on iOS, then I recommend the open source Finch.
I hope this helps!
You can synthesize waveforms of your desired frequency and feed them to the callbacks of either the Audio Queue or the RemoteIO Audio Unit API.
Here is a short tutorial on some of the code needed to create sine wave tones for iOS in C.

detecting heartbeat peakpower using iphone sdk?

i want to detect heart rate using iphone sdk does someone knows any method for calculating heartbeat rate?
Fast Fourier Transform is a class of algorithms that can quickly turn samples into an analysis that tells you how prominently ceratin frequencies occur in that sample. For more check out:
Wikipedia: FFT
Literate program example: Cooley-Tukey FFT
This is relevant to your problem because: (1) heart rate is itself a frequency, and (2) most of the sound that comes through the body that you can measure will be within a certain frequency range. Dropping frequencies outside this range means dropping all or mostly noise.
Good luck!
Well I've seen various implementations. Some of them use the accelerometer to detect minute movements in your arm/hand when you hold the phone, some of them can use the microphone, you could also do a manual 'tap' interface where you tap the screen while checking your own pulse.