1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
* SampleFormatOpus.cpp
* --------------------
* Purpose: Opus sample import.
* Notes :
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Sndfile.h"
#ifndef MODPLUG_NO_FILESAVE
#include "../common/mptFileIO.h"
#endif
#include "../common/misc_util.h"
#include "Tagging.h"
#include "Loaders.h"
#include "ChunkReader.h"
#include "modsmp_ctrl.h"
#include "../soundbase/SampleFormatConverters.h"
#include "../soundbase/SampleFormatCopy.h"
#include "../soundlib/ModSampleCopy.h"
//#include "../common/mptCRC.h"
#include "OggStream.h"
#ifdef MPT_WITH_OGG
#include <ogg/ogg.h>
#endif // MPT_WITH_OGG
#if defined(MPT_WITH_OPUSFILE)
#include <opusfile.h>
#endif
OPENMPT_NAMESPACE_BEGIN
////////////////////////////////////////////////////////////////////////////////
// Opus
#if defined(MPT_WITH_OPUSFILE)
static mpt::ustring UStringFromOpus(const char *str)
{
return str ? mpt::ToUnicode(mpt::CharsetUTF8, str) : mpt::ustring();
}
static FileTags GetOpusFileTags(OggOpusFile *of)
{
FileTags tags;
const OpusTags *ot = op_tags(of, -1);
if(!ot)
{
return tags;
}
tags.encoder = UStringFromOpus(opus_tags_query(ot, "ENCODER", 0));
tags.title = UStringFromOpus(opus_tags_query(ot, "TITLE", 0));
tags.comments = UStringFromOpus(opus_tags_query(ot, "DESCRIPTION", 0));
tags.bpm = UStringFromOpus(opus_tags_query(ot, "BPM", 0)); // non-standard
tags.artist = UStringFromOpus(opus_tags_query(ot, "ARTIST", 0));
tags.album = UStringFromOpus(opus_tags_query(ot, "ALBUM", 0));
tags.trackno = UStringFromOpus(opus_tags_query(ot, "TRACKNUMBER", 0));
tags.year = UStringFromOpus(opus_tags_query(ot, "DATE", 0));
tags.url = UStringFromOpus(opus_tags_query(ot, "CONTACT", 0));
tags.genre = UStringFromOpus(opus_tags_query(ot, "GENRE", 0));
return tags;
}
#endif // MPT_WITH_OPUSFILE
bool CSoundFile::ReadOpusSample(SAMPLEINDEX sample, FileReader &file)
{
file.Rewind();
#if defined(MPT_WITH_OPUSFILE)
int rate = 0;
int channels = 0;
std::vector<int16> raw_sample_data;
std::string sampleName;
FileReader initial = file.GetChunk(65536); // 512 is recommended by libopusfile
if(op_test(NULL, initial.GetRawData<unsigned char>(), initial.GetLength()) != 0)
{
return false;
}
OggOpusFile *of = op_open_memory(file.GetRawData<unsigned char>(), file.GetLength(), NULL);
if(!of)
{
return false;
}
rate = 48000;
channels = op_channel_count(of, -1);
if(rate <= 0 || channels <= 0)
{
op_free(of);
of = NULL;
return false;
}
if(channels > 2 || op_link_count(of) != 1)
{
// We downmix multichannel to stereo as recommended by Opus specification in
// case we are not able to handle > 2 channels.
// We also decode chained files as stereo even if they start with a mono
// stream, which simplifies handling of link boundaries for us.
channels = 2;
}
sampleName = mpt::ToCharset(GetCharsetInternal(), GetSampleNameFromTags(GetOpusFileTags(of)));
std::vector<int16> decodeBuf(120 * 48000 / 1000); // 120ms (max Opus packet), 48kHz
bool eof = false;
while(!eof)
{
int framesRead = 0;
if(channels == 2)
{
framesRead = op_read_stereo(of, &(decodeBuf[0]), static_cast<int>(decodeBuf.size()));
} else if(channels == 1)
{
framesRead = op_read(of, &(decodeBuf[0]), static_cast<int>(decodeBuf.size()), NULL);
}
if(framesRead > 0)
{
raw_sample_data.insert(raw_sample_data.end(), decodeBuf.begin(), decodeBuf.begin() + (framesRead * channels));
} else if(framesRead == 0)
{
eof = true;
} else if(framesRead == OP_HOLE)
{
// continue
} else
{
// other errors are fatal, stop decoding
eof = true;
}
if((raw_sample_data.size() / channels) > MAX_SAMPLE_LENGTH)
{
break;
}
}
op_free(of);
of = NULL;
if(raw_sample_data.empty())
{
return false;
}
if((raw_sample_data.size() / channels) > MAX_SAMPLE_LENGTH)
{
return false;
}
DestroySampleThreadsafe(sample);
mpt::String::Copy(m_szNames[sample], sampleName);
Samples[sample].Initialize();
Samples[sample].nC5Speed = rate;
Samples[sample].nLength = mpt::saturate_cast<SmpLength>(raw_sample_data.size() / channels);
Samples[sample].uFlags.set(CHN_16BIT);
Samples[sample].uFlags.set(CHN_STEREO, channels == 2);
if(!Samples[sample].AllocateSample())
{
return false;
}
std::copy(raw_sample_data.begin(), raw_sample_data.end(), Samples[sample].sample16());
Samples[sample].Convert(MOD_TYPE_IT, GetType());
Samples[sample].PrecomputeLoops(*this, false);
return true;
#else // !MPT_WITH_OPUSFILE
MPT_UNREFERENCED_PARAMETER(sample);
MPT_UNREFERENCED_PARAMETER(file);
return false;
#endif // MPT_WITH_OPUSFILE
}
OPENMPT_NAMESPACE_END
|