Some hard questions (maybe?)

Hi guys! I'm new to this forum and community. Wanted to take a second to introduce myself here. I'm a software developer specializing in web development. Most of my career I've used c# religiously. However I've always wanted to take the time to learn c++, which is exactly what I'm doing now!

So anyways. I've recently grabbed a raspberry pi and took the opportunity to do some projects using wiringPi in c++. Most of what I wanted to do has been fairly straightforward. However the last few days I've sort of hit a wall. What im trying to seems extremely difficult.

So the project is a tetris game using LEDs in a grid pattern laid out across a breadboard. I've got a good handle on that part as its fairly easy to plug in some shift registers and write the code to handle the game logic.

The issue is, I'm wanting to write some native audio code. Basically, I want to parse a wav file(8bit, PCM), read its contents. And translate that to frequencies between 0-8000( the limit of my passive speakers)

I have already written the notes to play for tretris myself and that works fine, but actually reading it from a wav file is part of what I wanted to accomplish, so it feels like cheating to just write the notes myself.

So far, I've been able to read the headers correctly. And I've got data chunk loaded into memory.. And I've been told I need to use a fft to translate that data to readable frequencies. Im not sure exactly how that's done. I've ran the data through fftw3 but when I play it's output it sounds like a garbbeled mess. I've also tried using libsnd library to convert it, and that did return a list of frequencies between 1 and -1. But I'm not sure where to go from there, as wiringPI can't set a pin to a negative voltage

I know this was long. I thank anyone who took the time to read it! I'll try to track the progress of my project on this thread so you guys can see it come together! Thank you immensely!

Last edited on
I've ran the data through fftw3 but when I play it's output it sounds like a garbbeled mess.
An FFT is never going to give you the final buffer to send to a note-based audio interface. You'll still need to interpret its output to decide what to use and what not to use.

I've also tried using libsnd library to convert it, and that did return a list of frequencies between 1 and -1.
I very much doubt that. Even a Fourier transform doesn't convert a PCM into a list of frequencies, not to mention that a negative frequency is meaningless. What you got was most likely the PCM converted to float samples.
I suggest making a test WAV file that just makes a constant, pure frequency like 440 Hz, and practice using fft to check that you're able to determine the frequency of that sine wave.

Negative frequencies just show up mathematically due to how the fft works on real signals. You'll have a matching negative and positive frequency at the bin corresponding to around 440 Hz, for example.

I've ran the data through fftw3 but when I play it's output it sounds like a garbbeled mess
What do you mean by "play its output"? You can't play the fft of a signal.

If you're trying to convert something sampled at ~40k Hz to something sampled at 16k Hz, then I suppose you'd first want a low-pass filter to remove the frequencies above 8k Hz.

Some random links:
https://dsp.stackexchange.com/questions/26927/what-is-a-frequency-bin
https://stackoverflow.com/questions/12040464/the-length-of-signal-in-calculating-fft
Last edited on
I did figure there were more steps to converting it over then just running it through an FFT. Im just not sure how to approach those steps!

Libsnd is a library for parsing audio files. Im pretty its doing more then just an FFT to get it to that list of float values. I did throw out libsnd though as I wanted to do it myself.

How would one go about interpreting the audio samples given by the FFT?
I suggest making a test WAV file that just makes a constant, pure frequency like 440 Hz, and practice using fft to check that you're able to determine the frequency of that sine wave.


Thats actually an excellent idea. I think that may be how I proceed forward at the moment.

My current thought process was to try and graph the audio file on a spectrogram. That way I can see that I'm processing it correctly.

What do you mean by "play its output"? You can't play the fft of a signal.


Sorry I should have been more clear. I'm using a function for the output of the fft.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void calculateFrequencies(fftw_complex* data, size_t len, int Fs) {
	for (int i = 0; i < len; i++) {
		int real, imaginary;
		float freq, magnitude;
		int index;

		real = data[i][0];
		imaginary = data[i][1];

		magnitude = sqrt(real * real + imaginary * imaginary);
		freq = (i * Fs)/ len;

		index = freq / 1000;//round(freq);
		if (index <= MAX_FREQ) {
			freqMagnitude[index] += magnitude;
		}
	}
}
Topic archived. No new replies allowed.