MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / Convert

Method Convert

Source/Engine/Tools/AudioTool/MP3Decoder.cpp:10–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <minimp3/minimp3.h>
9
10bool MP3Decoder::Convert(ReadStream* stream, AudioDataInfo& info, Array<byte>& result, uint32 offset)
11{
12 ASSERT(stream);
13
14 mStream = stream;
15 mStream->SetPosition(offset);
16
17 int32 dataSize = mStream->GetLength() - offset;
18 Array<byte> dataBytes;
19 dataBytes.Resize(dataSize);
20 byte* data = dataBytes.Get();
21 mStream->ReadBytes(data, dataSize);
22
23 info.NumSamples = 0;
24 info.SampleRate = 0;
25 info.NumChannels = 0;
26 info.BitDepth = 16;
27
28 mp3dec_frame_info_t mp3Info;
29 short pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
30
31 MemoryWriteStream output(Math::RoundUpToPowerOf2(dataSize));
32
33 do
34 {
35 const int32 samples = mp3dec_decode_frame(&mp3d, data, dataSize, pcm, &mp3Info);
36 if (samples)
37 {
38 info.NumSamples += samples * mp3Info.channels;
39
40 output.WriteBytes(pcm, samples * 2 * mp3Info.channels);
41
42 if (!info.SampleRate)
43 info.SampleRate = mp3Info.hz;
44 if (!info.NumChannels)
45 info.NumChannels = mp3Info.channels;
46 if (info.SampleRate != mp3Info.hz || info.NumChannels != mp3Info.channels)
47 break;
48 }
49 data += mp3Info.frame_bytes;
50 dataSize -= mp3Info.frame_bytes;
51 } while (mp3Info.frame_bytes);
52
53 if (info.SampleRate == 0)
54 return true;
55
56 // Load the whole audio data
57 const int32 bytesPerSample = info.BitDepth / 8;
58 const int32 bufferSize = info.NumSamples * bytesPerSample;
59 result.Set(output.GetHandle(), bufferSize);
60
61 return false;
62}
63
64bool MP3Decoder::Open(ReadStream* stream, AudioDataInfo& info, uint32 offset)
65{

Callers 4

ExtractDataRawMethod · 0.45
WriteBufferMethod · 0.45
ImportMethod · 0.45
ExportAudioClipMethod · 0.45

Calls 10

mp3dec_decode_frameFunction · 0.85
RoundUpToPowerOf2Function · 0.50
SetPositionMethod · 0.45
GetLengthMethod · 0.45
ResizeMethod · 0.45
GetMethod · 0.45
ReadBytesMethod · 0.45
WriteBytesMethod · 0.45
SetMethod · 0.45
GetHandleMethod · 0.45

Tested by

no test coverage detected