gpt4 book ai didi

audio - 使用SDL Mixer的比赛持续时间

转载 作者:行者123 更新时间:2023-12-03 02:16:48 25 4
gpt4 key购买 nike

我们定义了以下基本代码。相应地按下按键时,这些代码片段将播放音乐卡盘。现在的问题是file1较短,而file2稍长。我们需要将两者混合并播放,以延长file1的持续时间以匹配到file2。如何通过sdl进行操作?

Mix_Chunk *file1 = NULL;
Mix_Chunk *file2 = NULL;

file1 = Mix_LoadWAV( "file1.wav" );
file2 = Mix_LoadWAV( "file2.wav" );

while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//If 1 was pressed
if( event.key.keysym.sym == SDLK_1 )
{
//Play the scratch effect
if( Mix_PlayChannel( -1, file1, 0 ) == -1 )
{
return 1;
}
}
//If 2 was pressed
else if( event.key.keysym.sym == SDLK_2 )
{
//Play the high hit effect
if( Mix_PlayChannel( -1, file2, 0 ) == -1 )
{
return 1;
}
}

}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

最佳答案

您将必须进行一些上采样或下采样。在您的情况下,您可能希望对file1进行升采样以使其更长。就是这样:

// This function add samples from the original audio according to the value of "rate".
// Since the sampling rate remains the same, effectively it will cause the audio to playback slower.
void Play_UpSample (int channel, Mix_Chunk *sample, int loop, int rate)
{
int m, n;
Mix_Chunk *newsample = new Mix_Chunk;
int original_length = sample->alen;
int length = original_length * rate;

newsample->allocated = sample->allocated;
newsample->abuf = NULL;
newsample->alen = length;
newsample->volume = sample->volume;
Uint8 *data1 = new Uint8[length];
Uint8 *start = sample->abuf;
Uint8 *start1 = data1;

int size = Get_Format();

if (size == 32) {
Uint32 sam;

for (m=1; m<=original_length; m+=4) {
sam = *((Uint32 *)start);
for (n=1; n<=rate; n++) {
*((Uint32 *)start1) = sam;
start1+=4;
}
start+=4;
}
}
else if (size == 16) {
Uint16 sam;
for (m=1; m<=original_length; m+=2) {
sam = *((Uint16 *)start);
for (n=1; n<=rate; n++) {
*((Uint16 *)start1) = sam;
start1+=2;
}
start+=2;
}

}
else if (size == 8) {
Uint8 sam;
for (m=1; m<=original_length; m+=4) {
sam = *start;
for (n=1; n<=rate; n++) {
*start1 = sam;
start1++;
}
start++;
}


}
newsample->abuf = data1;

Mix_PlayChannel(channel, newsample, loop);
}

关于audio - 使用SDL Mixer的比赛持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19200033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com