diff --git a/main.cc b/main.cc index 2a2b25c..210d5b4 100644 --- a/main.cc +++ b/main.cc @@ -12,6 +12,9 @@ #include "audioDecoder.h" using std::cout; +constexpr int SCREEN_WIDTH = 640; +constexpr int SCREEN_HEIGHT = 480; + struct OpenglVideoParam { SDL_GLContext glContext; @@ -19,17 +22,20 @@ struct OpenglVideoParam unsigned int texs[3]; }; -int InitAudio(const char* targetFilePath, MediaParam& param) +int InitAudio(SDL_Window* window, SDL_Renderer* renderer, const char* targetFilePath, MediaParam& param) { InitDecoder(targetFilePath, param); + window = SDL_CreateWindow("mp", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + renderer= SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); std::jthread(RequestAudioPacket, std::ref(param)).detach(); SDL_AudioSpec des; des.freq = param.audioParam.codecCtx->sample_rate; - des.channels = param.audioParam.codecCtx->channels; + des.channels = param.audioParam.codecCtx->ch_layout.nb_channels; des.format = AUDIO_S16SYS; des.samples = 1024; des.silence = 0; - des.userdata = &(param.audioParam); + std::tuple* callbackParam = new std::tuple{window, renderer, &(param.audioParam)}; + des.userdata = callbackParam; des.callback = audioCallback; if (SDL_OpenAudio(&des, nullptr) < 0) { @@ -221,7 +227,7 @@ int main(int argc, char** argv) } case FileType::AUDIO: { - InitAudio(targetFilepath, mediaParam); + InitAudio(window, renderer, targetFilepath, mediaParam); break; } case FileType::ERRORTYPE: diff --git a/src/audioDecoder.cc b/src/audioDecoder.cc index 2b84f59..273950b 100644 --- a/src/audioDecoder.cc +++ b/src/audioDecoder.cc @@ -3,13 +3,14 @@ #include #include #include +#include extern "C" { #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, SDL_Window* window, SDL_Renderer* renderer) { AVFrame* frame = av_frame_alloc(); int dataSize = 0; @@ -69,19 +70,41 @@ int RequestAudioFrame(AudioParam& param, uint8_t* audioBuffer, int bufSize) const uint64_t dstNbSamples = av_rescale_rnd(swr_get_delay(swrCtx, frame->sample_rate) + frame->nb_samples, frame->sample_rate, frame->sample_rate, static_cast(1)); const int nb = swr_convert(swrCtx, &audioBuffer, static_cast(dstNbSamples), const_cast(frame->data), frame->nb_samples); dataSize = frame->ch_layout.nb_channels * nb * av_get_bytes_per_sample(dstFormat); + + //render wave + int nbSamples = frame->nb_samples; + fftw_complex* in = static_cast(fftw_malloc(sizeof(fftw_complex) * nbSamples)); + fftw_complex* out = static_cast(fftw_malloc(sizeof(fftw_complex) * nbSamples)); + + for(int i= 0; i < nbSamples; i++) { + in[i][0] = frame->data[0][i]; + in[i][1] = 0; + } + + fftw_plan p = fftw_plan_dft_1d(nbSamples, in, out, FFTW_FORWARD, FFTW_ESTIMATE); + fftw_execute(p); + fftw_destroy_plan(p); + //TODO: render wave + + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xff); + SDL_RenderClear(renderer); + SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff); + SDL_RenderDrawLine(renderer, 0, 0, 300, 300); + SDL_RenderPresent(renderer); av_frame_free(&frame); swr_free(&swrCtx); return dataSize; } void audioCallback(void* userdata, uint8_t* stream, int len) { - AudioParam* param = static_cast(userdata); + const auto callbackParam = *static_cast*>(userdata); + const auto param = std::get<2>(callbackParam); SDL_memset(stream, 0, len); while (len > 0) { if (param->bufferIndex >= param->bufferSize) { - const int audioSize = RequestAudioFrame(*param, param->buffer, sizeof(param->buffer)); + const int audioSize = RequestAudioFrame(*param, param->buffer, sizeof(param->buffer), std::get<0>(callbackParam), std::get<1>(callbackParam)); if (audioSize < 0) { param->bufferSize = 0;