Files
XCEngine/MVS/Music fluctuations/source/scene/MainScene/MainScene.cpp

165 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include"MainScene.h"
#include"../../global/Base.h"
#include <limits>
void MainScene::OnEnter()
{
std::cout << "enter——mainScene" << std::endl;
m_sunshine = "res/sound/cat.wav";
m_context->m_wavFileReader.OpenFile(m_sunshine);
std::cout << m_context->m_wavFileReader.GetSampleRate() << std::endl;
std::cout << m_context->m_wavFileReader.GetLastingTime() << std::endl;
std::cout << m_context->m_wavFileReader.GetSampleNum() << std::endl;
std::cout << m_context->m_wavFileReader.GetDataLength() << std::endl;
std::cout << m_context->m_wavFileReader.GetBitsPerSample() << std::endl;
std::cout << m_context->m_wavFileReader.GetBytesPerSec() << std::endl;
std::cout << m_context->m_wavFileReader.GetChannels() << std::endl;
int samplenum = (int)m_context->m_wavFileReader.GetSampleNum() / 2 / rate;
//滤波器初始化长度为8192
filter = new FFTFilter(8192);
/*
44100
108.668
9584548两个声道一共
19169096
16
176400
2
*/
}
void MainScene::OnExit()
{
std::cout << "exit——mainScene" << std::endl;
}
int drawCount = 8192;
int sampleIndex = -drawCount / 2;
float time2 = 0;
float duanIndex = 0;
bool p = true;
void MainScene::Update(float p_deltaTime)
{
if (p) { p = false; audio_source = m_context->m_audioManager.Load("res/sound/cat.wav");
audio_source->Play();
audio_source->StartEnergyDetect();
return;
}
time2 += p_deltaTime;
sampleIndex += p_deltaTime * m_context->m_wavFileReader.GetSampleRate();
duanIndex += p_deltaTime * m_context->m_wavFileReader.GetSampleRate() / (float)1024 ;
//std::cout << duanIndex <<" " << rates[(int)duanIndex] << " "<<d[(int)duanIndex] << std::endl;
m_context->m_gpu.drawVerticalLine(30, 5, 10, RGBA(255, 0, 0, 255));
m_context->m_gpu.drawVerticalLine(60, 5, 10, RGBA(255, 0, 0, 255));
m_context->m_gpu.drawVerticalLine(90, 5, 10, RGBA(255, 0, 0, 255));
m_context->m_gpu.drawVerticalLine(120, 5, 10, RGBA(255, 0, 0, 255));
m_context->m_gpu.drawVerticalLine(150, 5, 10, RGBA(255, 0, 0, 255));
//for (int i = 0; i < 30* rates[(int)duanIndex]; i++)
//{
// m_context->m_gpu.drawVerticalLine(i, 0, 5, RGBA(0, 255, 0, 255));
//}
for (int i = 0; i < 30 * audio_source->GetEnergy(); i++)
{
m_context->m_gpu.drawVerticalLine(i, 0, 5, RGBA(0, 255, 0, 255));
}
/*时域转频域*/
std::vector<float> samples;
std::vector<float> fresamples(8192, 0.f);
for (int i = 0; i < drawCount; i++)
{
float sample = m_context->m_wavFileReader.GetSampleValue((i + sampleIndex) * 2);
samples.push_back(sample);
}
filter->ForwardTransform(samples, &fresamples);
int r = sin(time2) * 255;
for (int i = 0; i < drawCount; i++)
{
int x = i / (float)drawCount * 800;
//int y = m_context->m_wavFileReader.GetSampleValue((i + sampleIndex) * 2) / (float)65535 * 600 + 300;
int y = fresamples[i] / (float)655350 * 200 + 300;
if (x > 800)
{
m_context->m_gpu.drawPoint(x, y, RGBA(255, 255, 255, 255));
}
else
{
if (y > 300)
{
m_context->m_gpu.drawVerticalLine(x, 300, y, RGBA(r, x / (float)800 * 255, y / (float)600 * 255, 255));
}
else
{
m_context->m_gpu.drawVerticalLine(x, y, 300, RGBA(r, x / (float)800 * 255, y / (float)600 * 255, 255));
}
}
}
m_context->m_gpu.drawVerticalLine(400, 0, 600, RGBA(200, 0, 0, 255));
if (audio_source->GetEnergy() > 2.0f)
{
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
if (pow(i - 20, 2) + pow(j - 20, 2) < 400)
{
m_context->m_gpu.drawPoint(380 + i, 480 + j, RGBA(0, 255, 255, 255));
}
}
}
}
}
/*简单的波形图绘制*/
/*
int drawCount = 80000;
int sampleIndex = -drawCount/2;
void MainScene::Update(float p_deltaTime)
{
sampleIndex += p_deltaTime * m_context->m_wavFileReader.GetSampleRate();
for (int i = 0; i < drawCount; i++)
{
int x = i / (float)drawCount * 800;
int y = m_context->m_wavFileReader.GetSampleValue((i + sampleIndex)*2) / (float)65535 * 600 + 300;
if (x > 400)
{
m_context->m_gpu.drawPoint(x, y, RGBA(255, 255, 255, 255));
}
else
{
if (y > 300)
{
m_context->m_gpu.drawVerticalLine(x, 300,y, RGBA(120, 120, 120, 255));
}
else
{
m_context->m_gpu.drawVerticalLine(x, y, 300,RGBA(120, 120, 120, 255));
}
}
}
m_context->m_gpu.drawVerticalLine(400,0, 600, RGBA(200, 0, 0, 255));
}
*/