MCPcopy Create free account
hub / github.com/cocos/cocos-engine / decodeToPcm

Method decodeToPcm

native/cocos/audio/android/AudioDecoderOgg.cpp:43–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43bool AudioDecoderOgg::decodeToPcm() {
44 _fileData = FileUtils::getInstance()->getDataFromFile(_url);
45 if (_fileData.isNull()) {
46 return false;
47 }
48
49 ov_callbacks callbacks;
50 callbacks.read_func = AudioDecoder::fileRead;
51 callbacks.seek_func = AudioDecoderOgg::fseek64Wrap;
52 callbacks.close_func = AudioDecoder::fileClose;
53 callbacks.tell_func = AudioDecoder::fileTell;
54
55 _fileCurrPos = 0;
56
57 OggVorbis_File vf;
58 int ret = ov_open_callbacks(this, &vf, NULL, 0, callbacks);
59 if (ret != 0) {
60 ALOGE("Open file error, file: %s, ov_open_callbacks return %d", _url.c_str(), ret);
61 return false;
62 }
63 // header
64 auto vi = ov_info(&vf, -1);
65
66 uint32_t pcmSamples = (uint32_t)ov_pcm_total(&vf, -1);
67
68 uint32_t bufferSize = pcmSamples * vi->channels * sizeof(short);
69 char *pcmBuffer = (char *)malloc(bufferSize);
70 memset(pcmBuffer, 0, bufferSize);
71
72 int currentSection = 0;
73 long curPos = 0;
74 long readBytes = 0;
75
76 do {
77 readBytes = ov_read(&vf, pcmBuffer + curPos, 4096, &currentSection);
78 curPos += readBytes;
79 } while (readBytes > 0);
80
81 if (curPos > 0) {
82 _result.pcmBuffer->insert(_result.pcmBuffer->end(), pcmBuffer, pcmBuffer + bufferSize);
83 _result.numChannels = vi->channels;
84 _result.sampleRate = vi->rate;
85 _result.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
86 _result.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
87 _result.channelMask = vi->channels == 1 ? SL_SPEAKER_FRONT_CENTER : (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT);
88 _result.endianness = SL_BYTEORDER_LITTLEENDIAN;
89 _result.numFrames = pcmSamples;
90 _result.duration = 1.0f * pcmSamples / vi->rate;
91 } else {
92 ALOGE("ov_read returns 0 byte!");
93 }
94
95 ov_clear(&vf);
96 free(pcmBuffer);
97
98 return (curPos > 0);
99}
100

Callers

nothing calls this directly

Calls 6

mallocFunction · 0.85
getDataFromFileMethod · 0.80
endMethod · 0.65
freeFunction · 0.50
isNullMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected