audio support decode mp3.

This commit is contained in:
u0u0
2021-10-01 11:11:21 +08:00
parent 6d5255be0b
commit c33ae2c49b
9 changed files with 3360 additions and 0 deletions

View File

@@ -137,6 +137,7 @@ target_include_directories(cocos2d
PRIVATE ${COCOS2DX_ROOT_PATH}/cocos/editor-support/fairygui
PRIVATE ${COCOS2DX_ROOT_PATH}/external/libogg
PRIVATE ${COCOS2DX_ROOT_PATH}/external/libvorbis
PRIVATE ${COCOS2DX_ROOT_PATH}/external/minimp3
INTERFACE ${COCOS2DX_ROOT_PATH}/cocos/base
INTERFACE ${COCOS2DX_ROOT_PATH}/cocos/audio/include
INTERFACE ${COCOS2DX_ROOT_PATH}/cocos/platform/${PLATFORM_FOLDER}

View File

@@ -2,10 +2,12 @@ set(AUDIO_SRC
audio/RDAudio.cpp
audio/RDAudioOgg.cpp
audio/RDAudioAAC.cpp
audio/RDAudioMP3.cpp
)
set(AUDIO_HEADER
audio/RDAudio.h
audio/RDAudioOgg.h
audio/RDAudioAAC.h
audio/RDAudioMP3.h
)

View File

@@ -18,6 +18,7 @@
#include "cocos/audio/RDAudio.h"
#include "cocos/audio/RDAudioOgg.h"
#include "cocos/audio/RDAudioAAC.h"
#include "cocos/audio/RDAudioMP3.h"
#include "cocos2d.h"
// singleton stuff
@@ -161,6 +162,13 @@ void RDAudio::threadLoop()
&asyncStruct->channels,
&asyncStruct->rate,
&asyncStruct->size);
} else if (ext == ".mp3") {
rtn = decodeMP3(data.getBytes(),
data.getSize(),
&asyncStruct->pcmData,
&asyncStruct->channels,
&asyncStruct->rate,
&asyncStruct->size);
}
if (rtn < 0) {

View File

@@ -0,0 +1,56 @@
// Copyright 2021 KeNan Liu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "RDAudioMP3.h"
#define MINIMP3_IMPLEMENTATION
#define MINIMP3_FLOAT_OUTPUT
#include "minimp3_ex.h"
int decodeMP3(unsigned char *mp3Data,
int mp3Size,
unsigned char **pcmData,
int *pcmChannels,
int *pcmRate,
int *pcmSize)
{
mp3dec_t mp3d;
mp3dec_file_info_t info;
memset(&info, 0, sizeof(info));
int ret = mp3dec_load_buf(&mp3d, mp3Data, mp3Size, &info, 0, 0);
if(0 != ret) {
printf("error: mp3dec_load_buf %d\n", ret);
return -1;
}
int size = info.samples * sizeof(int16_t);
int16_t* buffer = (int16_t *)malloc(size);
if (!buffer) {
printf("error: decodeMP3 not enough memory\n");
return -1;
}
mp3dec_f32_to_s16(info.buffer, buffer, info.samples);
free(info.buffer);
*pcmData = (unsigned char*)buffer;
*pcmChannels = info.channels;
*pcmRate = info.hz;
*pcmSize = size;
return 0;
}

33
cocos/audio/RDAudioMP3.h Normal file
View File

@@ -0,0 +1,33 @@
// Copyright 2021 KeNan Liu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __RDAudioMP3_H__
#define __RDAudioMP3_H__
#ifdef __cplusplus
extern "C" {
#endif
int decodeMP3(unsigned char *mp3Data,
int mp3Size,
unsigned char **pcmData,
int *pcmChannels,
int *pcmRate,
int *pcmSize);
#ifdef __cplusplus
}
#endif
#endif // __RDAudioMP3_H__

1865
external/minimp3/minimp3.h vendored Normal file

File diff suppressed because it is too large Load Diff

1394
external/minimp3/minimp3_ex.h vendored Normal file

File diff suppressed because it is too large Load Diff

BIN
tests/res/audio/effect.mp3 Normal file

Binary file not shown.

View File

@@ -60,6 +60,7 @@ function TestCase:ctor()
audio.loadFile("audio/bgm.ogg", loadedCB)
audio.loadFile("audio/effect.ogg", loadedCB)
audio.loadFile("audio/bgm.aac", loadedCB)
audio.loadFile("audio/effect.mp3", loadedCB)
btn:setVisible(false)
end
end)