fftw3 傅里叶变换求频率
This commit is contained in:
		@@ -1,10 +1,12 @@
 | 
				
			|||||||
#include "audioDecoder.h"
 | 
					#include "audioDecoder.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
#include <SDL2/SDL_audio.h>
 | 
					#include <SDL2/SDL_audio.h>
 | 
				
			||||||
#include <SDL2/SDL_stdinc.h>
 | 
					#include <SDL2/SDL_stdinc.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern "C" {
 | 
					extern "C" {
 | 
				
			||||||
#include "libswresample/swresample.h"
 | 
					#include "libswresample/swresample.h"
 | 
				
			||||||
 | 
					#include "fftw3.h"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize)
 | 
					int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize)
 | 
				
			||||||
@@ -37,6 +39,25 @@ int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize)
 | 
				
			|||||||
	if (frame->ch_layout.nb_channels > 0) {
 | 
						if (frame->ch_layout.nb_channels > 0) {
 | 
				
			||||||
		av_channel_layout_default(&frame->ch_layout, frame->ch_layout.nb_channels);
 | 
							av_channel_layout_default(&frame->ch_layout, frame->ch_layout.nb_channels);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						int nbSamples = frame->nb_samples;
 | 
				
			||||||
 | 
						uint8_t* audioData = frame->data[0];
 | 
				
			||||||
 | 
						fftw_complex* fftwInput = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * frame->nb_samples));
 | 
				
			||||||
 | 
						fftw_complex* fftwOutput = static_cast<fftw_complex*>(fftw_malloc(sizeof(fftw_complex) * frame->nb_samples));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (int i = 0; i < nbSamples; i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							fftwInput[i][0] = audioData[i];
 | 
				
			||||||
 | 
							fftwInput[i][1] = .0f;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fftw_plan fftwPlan = fftw_plan_dft_1d(nbSamples, fftwInput, fftwOutput, FFTW_FORWARD, FFTW_ESTIMATE);
 | 
				
			||||||
 | 
						fftw_execute(fftwPlan);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (int i = 0; i < nbSamples; i++)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							double magnitude = sqrt(fftwOutput[i][0] * fftwOutput[i][0] + fftwOutput[i][1] * fftwOutput[i][1]);
 | 
				
			||||||
 | 
							double phase = atan2(fftwOutput[i][1], fftwOutput[i][0]);
 | 
				
			||||||
 | 
							std::cout << "mag: " << magnitude << "\n phase: " << phase << "\n";
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	constexpr AVSampleFormat dstFormat = AV_SAMPLE_FMT_S16;
 | 
						constexpr AVSampleFormat dstFormat = AV_SAMPLE_FMT_S16;
 | 
				
			||||||
	swr_alloc_set_opts2(&swrCtx, &frame->ch_layout, dstFormat, frame->sample_rate, &frame->ch_layout, static_cast<AVSampleFormat>(frame->format), frame->sample_rate, 0, nullptr);
 | 
						swr_alloc_set_opts2(&swrCtx, &frame->ch_layout, dstFormat, frame->sample_rate, &frame->ch_layout, static_cast<AVSampleFormat>(frame->format), frame->sample_rate, 0, nullptr);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user