1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-13 14:13:32 +02:00

Import functions return containers of smart pointers to new tracks

This commit is contained in:
Paul Licameli
2016-03-02 12:52:13 -05:00
parent bc5519a0e2
commit 824ff647f8
16 changed files with 303 additions and 333 deletions

View File

@@ -354,9 +354,9 @@ ExtImportItem *Importer::CreateDefaultImportItem()
}
// returns number of tracks imported
int Importer::Import(const wxString &fName,
bool Importer::Import(const wxString &fName,
TrackFactory *trackFactory,
Track *** tracks,
TrackHolders &tracks,
Tags *tags,
wxString &errorMessage)
{
@@ -364,7 +364,6 @@ int Importer::Import(const wxString &fName,
pProj->mbBusyImporting = true;
ImportFileHandle *inFile = NULL;
int numTracks = 0;
wxString extension = fName.AfterLast(wxT('.'));
@@ -547,7 +546,7 @@ int Importer::Import(const wxString &fName,
{
delete inFile;
pProj->mbBusyImporting = false;
return 0;
return false;
}
}
// One stream - import it by default
@@ -556,7 +555,7 @@ int Importer::Import(const wxString &fName,
int res;
res = inFile->Import(trackFactory, tracks, &numTracks, tags);
res = inFile->Import(trackFactory, tracks, tags);
delete inFile;
@@ -566,21 +565,21 @@ int Importer::Import(const wxString &fName,
if (extension.IsSameAs(wxT("lof"), false))
{
pProj->mbBusyImporting = false;
return 1;
return true;
}
if (numTracks > 0)
if (tracks.size() > 0)
{
// success!
pProj->mbBusyImporting = false;
return numTracks;
return true;
}
}
if (res == eProgressCancelled || res == eProgressFailed)
{
pProj->mbBusyImporting = false;
return 0;
return false;
}
// We could exit here since we had a match on the file extension,
@@ -606,7 +605,7 @@ int Importer::Import(const wxString &fName,
unusableImportPlugin->
GetPluginFormatDescription().c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
unusableImporterNode = unusableImporterNode->GetNext();
}
@@ -618,7 +617,7 @@ int Importer::Import(const wxString &fName,
if ((extension.IsSameAs(wxT("midi"), false))||(extension.IsSameAs(wxT("mid"), false))) {
errorMessage.Printf(_("\"%s\" \nis a MIDI file, not an audio file. \nAudacity cannot open this type of file for playing, but you can\nedit it by clicking File > Import > MIDI."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
#endif
@@ -629,87 +628,87 @@ int Importer::Import(const wxString &fName,
/* i18n-hint: %s will be the filename */
errorMessage.Printf(_("\"%s\" is an audio CD track. \nAudacity cannot open audio CDs directly. \nExtract (rip) the CD tracks to an audio format that \nAudacity can import, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// playlist type files
if ((extension.IsSameAs(wxT("m3u"), false))||(extension.IsSameAs(wxT("ram"), false))||(extension.IsSameAs(wxT("pls"), false))) {
errorMessage.Printf(_("\"%s\" is a playlist file. \nAudacity cannot open this file because it only contains links to other files. \nYou may be able to open it in a text editor and download the actual audio files."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
//WMA files of various forms
if ((extension.IsSameAs(wxT("wma"), false))||(extension.IsSameAs(wxT("asf"), false))) {
errorMessage.Printf(_("\"%s\" is a Windows Media Audio file. \nAudacity cannot open this type of file due to patent restrictions. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
//AAC files of various forms (probably not encrypted)
if ((extension.IsSameAs(wxT("aac"), false))||(extension.IsSameAs(wxT("m4a"), false))||(extension.IsSameAs(wxT("m4r"), false))||(extension.IsSameAs(wxT("mp4"), false))) {
errorMessage.Printf(_("\"%s\" is an Advanced Audio Coding file. \nAudacity cannot open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// encrypted itunes files
if ((extension.IsSameAs(wxT("m4p"), false))) {
errorMessage.Printf(_("\"%s\" is an encrypted audio file. \nThese typically are from an online music store. \nAudacity cannot open this type of file due to the encryption. \nTry recording the file into Audacity, or burn it to audio CD then \nextract the CD track to a supported audio format such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// Real Inc. files of various sorts
if ((extension.IsSameAs(wxT("ra"), false))||(extension.IsSameAs(wxT("rm"), false))||(extension.IsSameAs(wxT("rpm"), false))) {
errorMessage.Printf(_("\"%s\" is a RealPlayer media file. \nAudacity cannot open this proprietary format. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// Other notes-based formats
if ((extension.IsSameAs(wxT("kar"), false))||(extension.IsSameAs(wxT("mod"), false))||(extension.IsSameAs(wxT("rmi"), false))) {
errorMessage.Printf(_("\"%s\" is a notes-based file, not an audio file. \nAudacity cannot open this type of file. \nTry converting it to an audio file such as WAV or AIFF and \nthen import it, or record it into Audacity."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// MusePack files
if ((extension.IsSameAs(wxT("mp+"), false))||(extension.IsSameAs(wxT("mpc"), false))||(extension.IsSameAs(wxT("mpp"), false))) {
errorMessage.Printf(_("\"%s\" is a Musepack audio file. \nAudacity cannot open this type of file. \nIf you think it might be an mp3 file, rename it to end with \".mp3\" \nand try importing it again. Otherwise you need to convert it to a supported audio \nformat, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// WavPack files
if ((extension.IsSameAs(wxT("wv"), false))||(extension.IsSameAs(wxT("wvc"), false))) {
errorMessage.Printf(_("\"%s\" is a Wavpack audio file. \nAudacity cannot open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// AC3 files
if ((extension.IsSameAs(wxT("ac3"), false))) {
errorMessage.Printf(_("\"%s\" is a Dolby Digital audio file. \nAudacity cannot currently open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// Speex files
if ((extension.IsSameAs(wxT("spx"), false))) {
errorMessage.Printf(_("\"%s\" is an Ogg Speex audio file. \nAudacity cannot currently open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// Video files of various forms
if ((extension.IsSameAs(wxT("mpg"), false))||(extension.IsSameAs(wxT("mpeg"), false))||(extension.IsSameAs(wxT("avi"), false))||(extension.IsSameAs(wxT("wmv"), false))||(extension.IsSameAs(wxT("rv"), false))) {
errorMessage.Printf(_("\"%s\" is a video file. \nAudacity cannot currently open this type of file. \nYou need to extract the audio to a supported format, such as WAV or AIFF."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// Audacity project
if (extension.IsSameAs(wxT("aup"), false)) {
errorMessage.Printf(_("\"%s\" is an Audacity Project file. \nUse the 'File > Open' command to open Audacity Projects."), fName.c_str());
pProj->mbBusyImporting = false;
return 0;
return false;
}
// we were not able to recognize the file type
@@ -735,7 +734,7 @@ int Importer::Import(const wxString &fName,
}
pProj->mbBusyImporting = false;
return 0;
return false;
}
//-------------------------------------------------------------------------

View File

@@ -11,6 +11,7 @@
#ifndef _IMPORT_
#define _IMPORT_
#include "ImportRaw.h" // defines TrackHolders
#include <vector>
#include <wx/arrstr.h>
#include <wx/string.h>
@@ -137,11 +138,10 @@ public:
*/
ExtImportItem *CreateDefaultImportItem();
// returns number of tracks imported
// if zero, the import failed and errorMessage will be set.
int Import(const wxString &fName,
// if false, the import failed and errorMessage will be set.
bool Import(const wxString &fName,
TrackFactory *trackFactory,
Track *** tracks,
TrackHolders &tracks,
Tags *tags,
wxString &errorMessage);

View File

@@ -206,8 +206,8 @@ public:
///! Imports audio
///\return import status (see Import.cpp)
int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags);
int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) override;
///! Reads next audio frame
///\return pointer to the stream context structure to which the frame belongs to or NULL on error, or 1 if stream is not to be imported.
@@ -272,7 +272,7 @@ private:
bool mCancelled; //!< True if importing was canceled by user
bool mStopped; //!< True if importing was stopped by user
wxString mName;
WaveTrack ***mChannels; //!< 2-dimentional array of WaveTrack's. First dimention - streams, second - channels of a stream. Length is mNumStreams
std::list<TrackHolders> mChannels; //!< 2-dimentional array of WaveTrack's. First dimention - streams, second - channels of a stream. Length is mNumStreams
#ifdef EXPERIMENTAL_OD_FFMPEG
bool mUsingOD;
#endif
@@ -348,7 +348,6 @@ FFmpegImportFileHandle::FFmpegImportFileHandle(const wxString & name)
mCancelled = false;
mStopped = false;
mName = name;
mChannels = NULL;
mProgressPos = 0;
mProgressLen = 1;
}
@@ -462,10 +461,10 @@ int FFmpegImportFileHandle::GetFileUncompressedBytes()
}
int FFmpegImportFileHandle::Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
CreateProgress();
@@ -484,10 +483,13 @@ int FFmpegImportFileHandle::Import(TrackFactory *trackFactory,
else i++;
}
mChannels = new WaveTrack **[mNumStreams];
mChannels.resize(mNumStreams);
for (int s = 0; s < mNumStreams; s++)
int s = -1;
for (auto &stream : mChannels)
{
++s;
switch (mScs[s]->m_stream->codec->sample_fmt)
{
case AV_SAMPLE_FMT_U8:
@@ -505,28 +507,30 @@ int FFmpegImportFileHandle::Import(TrackFactory *trackFactory,
// There is a possibility that number of channels will change over time, but we do not have WaveTracks for NEW channels. Remember the number of channels and stick to it.
mScs[s]->m_initialchannels = mScs[s]->m_stream->codec->channels;
mChannels[s] = new WaveTrack *[mScs[s]->m_stream->codec->channels];
int c;
for (c = 0; c < mScs[s]->m_stream->codec->channels; c++)
stream.resize(mScs[s]->m_stream->codec->channels);
int c = -1;
for (auto &channel : stream)
{
mChannels[s][c] = trackFactory->NewWaveTrack(mScs[s]->m_osamplefmt, mScs[s]->m_stream->codec->sample_rate).release();
++c;
channel = trackFactory->NewWaveTrack(mScs[s]->m_osamplefmt, mScs[s]->m_stream->codec->sample_rate);
if (mScs[s]->m_stream->codec->channels == 2)
{
switch (c)
{
case 0:
mChannels[s][c]->SetChannel(Track::LeftChannel);
mChannels[s][c]->SetLinked(true);
channel->SetChannel(Track::LeftChannel);
channel->SetLinked(true);
break;
case 1:
mChannels[s][c]->SetChannel(Track::RightChannel);
channel->SetChannel(Track::RightChannel);
break;
}
}
else
{
mChannels[s][c]->SetChannel(Track::MonoChannel);
channel->SetChannel(Track::MonoChannel);
}
}
}
@@ -534,8 +538,11 @@ int FFmpegImportFileHandle::Import(TrackFactory *trackFactory,
// Handles the start_time by creating silence. This may or may not be correct.
// There is a possibility that we should ignore first N milliseconds of audio instead. I do not know.
/// TODO: Nag FFmpeg devs about start_time until they finally say WHAT is this and HOW to handle it.
for (int s = 0; s < mNumStreams; s++)
s = -1;
for (auto &stream : mChannels)
{
++s;
int64_t stream_delay = 0;
if (mScs[s]->m_stream->start_time != int64_t(AV_NOPTS_VALUE) && mScs[s]->m_stream->start_time > 0)
{
@@ -544,9 +551,12 @@ int FFmpegImportFileHandle::Import(TrackFactory *trackFactory,
}
if (stream_delay != 0)
{
for (int c = 0; c < mScs[s]->m_stream->codec->channels; c++)
int c = -1;
for (auto &channel : stream)
{
WaveTrack *t = mChannels[s][c];
++c;
WaveTrack *t = channel.get();
t->InsertSilence(0,double(stream_delay)/AV_TIME_BASE);
}
}
@@ -662,38 +672,18 @@ int FFmpegImportFileHandle::Import(TrackFactory *trackFactory,
// Something bad happened - destroy everything!
if (res == eProgressCancelled || res == eProgressFailed)
{
for (int s = 0; s < mNumStreams; s++)
{
delete[] mChannels[s];
}
delete[] mChannels;
return res;
}
//else if (res == 2), we just stop the decoding as if the file has ended
*outNumTracks = 0;
for (int s = 0; s < mNumStreams; s++)
{
*outNumTracks += mScs[s]->m_initialchannels;
}
// Create NEW tracks
*outTracks = new Track *[*outNumTracks];
// Copy audio from mChannels to newly created tracks (destroying mChannels elements in process)
int trackindex = 0;
for (int s = 0; s < mNumStreams; s++)
for (auto &stream : mChannels)
{
for(int c = 0; c < mScs[s]->m_initialchannels; c++)
for(auto &channel : stream)
{
mChannels[s][c]->Flush();
(*outTracks)[trackindex++] = mChannels[s][c];
channel->Flush();
outTracks.push_back(std::move(channel));
}
delete[] mChannels[s];
}
delete[] mChannels;
// Save metadata
WriteMetadata(tags);
@@ -715,7 +705,8 @@ int FFmpegImportFileHandle::WriteData(streamContext *sc)
{
// Find the stream index in mScs array
int streamid = -1;
for (int i = 0; i < mNumStreams; i++)
auto iter = mChannels.begin();
for (int i = 0; i < mNumStreams; ++iter, ++i)
{
if (mScs[i] == sc)
{
@@ -793,9 +784,10 @@ int FFmpegImportFileHandle::WriteData(streamContext *sc)
}
// Write audio into WaveTracks
for (int chn=0; chn < nChannels; chn++)
auto iter2 = iter->begin();
for (int chn=0; chn < nChannels; ++iter2, ++chn)
{
mChannels[streamid][chn]->Append((samplePtr)tmp[chn],sc->m_osamplefmt,index);
iter2->get()->Append((samplePtr)tmp[chn],sc->m_osamplefmt,index);
free(tmp[chn]);
}

View File

@@ -153,8 +153,8 @@ public:
wxString GetFileDescription();
int GetFileUncompressedBytes();
int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags);
int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) override;
wxInt32 GetStreamCount(){ return 1; }
@@ -173,7 +173,7 @@ private:
FLAC__uint64 mSamplesDone;
bool mStreamInfoDone;
int mUpdateResult;
WaveTrack **mChannels;
TrackHolders mChannels;
ODDecodeFlacTask *mDecoderTask;
};
@@ -245,18 +245,19 @@ FLAC__StreamDecoderWriteStatus MyFLACFile::write_callback(const FLAC__Frame *fra
{
short *tmp=new short[frame->header.blocksize];
for (unsigned int chn=0; chn<mFile->mNumChannels; chn++) {
auto iter = mFile->mChannels.begin();
for (unsigned int chn=0; chn<mFile->mNumChannels; ++iter, ++chn) {
if (frame->header.bits_per_sample == 16) {
for (unsigned int s=0; s<frame->header.blocksize; s++) {
tmp[s]=buffer[chn][s];
}
mFile->mChannels[chn]->Append((samplePtr)tmp,
iter->get()->Append((samplePtr)tmp,
int16Sample,
frame->header.blocksize);
}
else {
mFile->mChannels[chn]->Append((samplePtr)buffer[chn],
iter->get()->Append((samplePtr)buffer[chn],
int24Sample,
frame->header.blocksize);
}
@@ -430,33 +431,34 @@ int FLACImportFileHandle::GetFileUncompressedBytes()
int FLACImportFileHandle::Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
wxASSERT(mStreamInfoDone);
CreateProgress();
mChannels = new WaveTrack *[mNumChannels];
mChannels.resize(mNumChannels);
unsigned long c;
for (c = 0; c < mNumChannels; c++) {
mChannels[c] = trackFactory->NewWaveTrack(mFormat, mSampleRate).release();
auto iter = mChannels.begin();
for (int c = 0; c < mNumChannels; ++iter, ++c) {
*iter = trackFactory->NewWaveTrack(mFormat, mSampleRate);
if (mNumChannels == 2) {
switch (c) {
case 0:
mChannels[c]->SetChannel(Track::LeftChannel);
mChannels[c]->SetLinked(true);
iter->get()->SetChannel(Track::LeftChannel);
iter->get()->SetLinked(true);
break;
case 1:
mChannels[c]->SetChannel(Track::RightChannel);
iter->get()->SetChannel(Track::RightChannel);
break;
}
}
else {
mChannels[c]->SetChannel(Track::MonoChannel);
iter->get()->SetChannel(Track::MonoChannel);
}
}
@@ -482,14 +484,15 @@ int FLACImportFileHandle::Import(TrackFactory *trackFactory,
if(useOD)
{
sampleCount fileTotalFrames = mNumSamples;
sampleCount maxBlockSize = mChannels[0]->GetMaxBlockSize();
sampleCount maxBlockSize = mChannels.begin()->get()->GetMaxBlockSize();
for (sampleCount i = 0; i < fileTotalFrames; i += maxBlockSize) {
sampleCount blockLen = maxBlockSize;
if (i + blockLen > fileTotalFrames)
blockLen = fileTotalFrames - i;
for (c = 0; c < mNumChannels; c++)
mChannels[c]->AppendCoded(mFilename, i, blockLen, c,ODTask::eODFLAC);
auto iter = mChannels.begin();
for (int c = 0; c < mNumChannels; ++c, ++iter)
iter->get()->AppendCoded(mFilename, i, blockLen, c, ODTask::eODFLAC);
mUpdateResult = mProgress->Update(i, fileTotalFrames);
if (mUpdateResult != eProgressSuccess)
@@ -497,9 +500,9 @@ int FLACImportFileHandle::Import(TrackFactory *trackFactory,
}
bool moreThanStereo = mNumChannels>2;
for (c = 0; c < mNumChannels; c++)
for (const auto &channel : mChannels)
{
mDecoderTask->AddWaveTrack(mChannels[c]);
mDecoderTask->AddWaveTrack(channel.get());
if(moreThanStereo)
{
//if we have 3 more channels, they get imported on seperate tracks, so we add individual tasks for each.
@@ -515,25 +518,17 @@ int FLACImportFileHandle::Import(TrackFactory *trackFactory,
if (mUpdateResult == eProgressFailed || mUpdateResult == eProgressCancelled) {
for(c = 0; c < mNumChannels; c++) {
delete mChannels[c];
}
delete[] mChannels;
return mUpdateResult;
}
*outNumTracks = mNumChannels;
*outTracks = new Track *[mNumChannels];
for (c = 0; c < mNumChannels; c++) {
mChannels[c]->Flush();
(*outTracks)[c] = mChannels[c];
for (const auto &channel : mChannels) {
channel->Flush();
}
delete[] mChannels;
outTracks.swap(mChannels);
tags->Clear();
size_t cnt = mFile->mComments.GetCount();
for (c = 0; c < cnt; c++) {
for (int c = 0; c < cnt; c++) {
wxString name = mFile->mComments[c].BeforeFirst(wxT('='));
wxString value = mFile->mComments[c].AfterFirst(wxT('='));
if (name.Upper() == wxT("DATE") && !tags->HasTag(TAG_YEAR)) {

View File

@@ -91,7 +91,7 @@ struct GStreamContext
GstElement *mConv; // Audio converter
GstElement *mSink; // Application sink
bool mUse; // True if this stream should be imported
WaveTrack **mChannels; // Array of WaveTrack pointers, one for each channel
TrackHolders mChannels; // Array of WaveTrack pointers, one for each channel
gint mNumChannels; // Number of channels
gdouble mSampleRate; // Sample rate
gchar *mType; // Audio type
@@ -130,9 +130,8 @@ public:
///! Imports audio
///\return import status (see Import.cpp)
int Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
Tags *tags);
TrackHolders &outTracks,
Tags *tags) override;
// =========================================================================
// Handled within the gstreamer threads
@@ -496,7 +495,7 @@ GStreamerImportFileHandle::OnPadAdded(GstPad *pad)
}
// Allocate a NEW stream context
GStreamContext *c = g_new0(GStreamContext, 1);
GStreamContext *c = new GStreamContext, 1;
if (!c)
{
WARN(mPipeline, ("OnPadAdded: unable to allocate stream context"));
@@ -663,7 +662,7 @@ GStreamerImportFileHandle::OnNewSample(GStreamContext *c, GstSample *sample)
//
// It is done here because, at least in the case of chained oggs,
// not all streams are known ahead of time.
if (c->mChannels == NULL)
if (c->mChannels.empty())
{
// Get the sample format...no need to release caps or structure
GstCaps *caps = gst_sample_get_caps(sample);
@@ -697,8 +696,8 @@ GStreamerImportFileHandle::OnNewSample(GStreamContext *c, GstSample *sample)
}
// Allocate the track array
c->mChannels = new WaveTrack *[c->mNumChannels];
if (!c->mChannels)
c->mChannels.resize(c->mNumChannels);
if (c->mChannels.size() != c->mNumChannels)
{
WARN(mPipeline, ("OnNewSample: unable to allocate track array"));
return;
@@ -809,17 +808,7 @@ GStreamerImportFileHandle::~GStreamerImportFileHandle()
// Remove any remaining channel data...will only happen if an error
// occurred during streaming.
if (c->mChannels)
{
for (int ch = 0; ch < c->mNumChannels; ch++)
{
if (c->mChannels[ch])
{
delete c->mChannels[ch];
}
}
delete[] c->mChannels;
}
c->mChannels.clear();
// Remove the appsink element
if (c->mSink)
@@ -840,7 +829,7 @@ GStreamerImportFileHandle::~GStreamerImportFileHandle()
}
// And finally get rid of the context
g_free(c);
delete c;
}
g_mutex_unlock(&mStreamsLock);
@@ -1014,10 +1003,11 @@ GStreamerImportFileHandle::GetFileUncompressedBytes()
// Import streams
int
GStreamerImportFileHandle::Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
// Save track factory pointer
mTrackFactory = trackFactory;
@@ -1125,34 +1115,33 @@ GStreamerImportFileHandle::Import(TrackFactory *trackFactory,
g_mutex_lock(&mStreamsLock);
// Count the total number of tracks collected
*outNumTracks = 0;
int outNumTracks = 0;
for (guint s = 0; s < mStreams->len; s++)
{
GStreamContext *c = (GStreamContext*)g_ptr_array_index(mStreams, s);
if (c->mChannels)
{
*outNumTracks += c->mNumChannels;
outNumTracks += c->mNumChannels;
}
}
// Create NEW tracks
*outTracks = new Track *[*outNumTracks];
outTracks.resize(outNumTracks);
// Copy audio from mChannels to newly created tracks (destroying mChannels in process)
int trackindex = 0;
for (guint s = 0; s < mStreams->len; s++)
{
GStreamContext *c = (GStreamContext*)g_ptr_array_index(mStreams, s);
if (c->mChannels)
if (c->mNumChannels)
{
for (int ch = 0; ch < c->mNumChannels; ch++)
{
c->mChannels[ch]->Flush();
(*outTracks)[trackindex++] = c->mChannels[ch];
outTracks[trackindex++] = std::move(c->mChannels[ch]);
}
delete [] c->mChannels;
c->mChannels = NULL;
c->mChannels.clear();
}
}
g_mutex_unlock(&mStreamsLock);

View File

@@ -125,8 +125,8 @@ public:
wxString GetFileDescription();
int GetFileUncompressedBytes();
int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags);
int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) override;
wxInt32 GetStreamCount(){ return 1; }
@@ -229,9 +229,11 @@ int LOFImportFileHandle::GetFileUncompressedBytes()
return 0;
}
int LOFImportFileHandle::Import(TrackFactory * WXUNUSED(trackFactory), Track *** WXUNUSED(outTracks),
int * WXUNUSED(outNumTracks), Tags * WXUNUSED(tags))
int LOFImportFileHandle::Import(TrackFactory * WXUNUSED(trackFactory), TrackHolders &outTracks,
Tags * WXUNUSED(tags))
{
outTracks.clear();
wxASSERT(mTextFile->IsOpened());
if(mTextFile->Eof())

View File

@@ -95,7 +95,7 @@ struct private_data {
wxFile *file; /* the file containing the mp3 data we're feeding the encoder */
unsigned char *inputBuffer;
TrackFactory *trackFactory;
WaveTrack **channels;
TrackHolders channels;
ProgressDialog *progress;
int numChannels;
int updateResult;
@@ -130,8 +130,8 @@ public:
wxString GetFileDescription();
int GetFileUncompressedBytes();
int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags);
int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) override;
wxInt32 GetStreamCount(){ return 1; }
@@ -202,10 +202,10 @@ int MP3ImportFileHandle::GetFileUncompressedBytes()
return 0;
}
int MP3ImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags)
int MP3ImportFileHandle::Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags)
{
int chn;
outTracks.clear();
CreateProgress();
@@ -214,7 +214,6 @@ int MP3ImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
mPrivateData.file = mFile;
mPrivateData.inputBuffer = new unsigned char [INPUT_BUFFER_SIZE];
mPrivateData.progress = mProgress.get();
mPrivateData.channels = NULL;
mPrivateData.updateResult= eProgressSuccess;
mPrivateData.id3checked = false;
mPrivateData.numChannels = 0;
@@ -236,13 +235,6 @@ int MP3ImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
if (!res) {
/* failure */
/* printf("failure\n"); */
/* DELETE everything */
for (chn = 0; chn < mPrivateData.numChannels; chn++) {
delete mPrivateData.channels[chn];
}
delete[] mPrivateData.channels;
return (mPrivateData.updateResult);
}
@@ -251,19 +243,16 @@ int MP3ImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
/* copy the WaveTrack pointers into the Track pointer list that
* we are expected to fill */
*outNumTracks = mPrivateData.numChannels;
*outTracks = new Track* [mPrivateData.numChannels];
for(chn = 0; chn < mPrivateData.numChannels; chn++) {
mPrivateData.channels[chn]->Flush();
(*outTracks)[chn] = mPrivateData.channels[chn];
}
delete[] mPrivateData.channels;
for(const auto &channel : mPrivateData.channels) {
channel->Flush();
}
outTracks.swap(mPrivateData.channels);
/* Read in any metadata */
ImportID3(tags);
return mPrivateData.updateResult;
}
return mPrivateData.updateResult;
}
MP3ImportFileHandle::~MP3ImportFileHandle()
{
@@ -472,7 +461,7 @@ enum mad_flow output_cb(void *_data,
int channels, samplerate;
sampleCount samples;
struct private_data *data = (struct private_data *)_data;
int chn, smpl;
int smpl;
samplerate= pcm->samplerate;
channels = pcm->channels;
@@ -482,22 +471,22 @@ enum mad_flow output_cb(void *_data,
* will hold the data. We do this now because now is the first
* moment when we know how many channels there are. */
if(!data->channels) {
data->channels = new WaveTrack* [channels];
if(data->channels.empty()) {
data->channels.resize(channels);
sampleFormat format = (sampleFormat) gPrefs->
Read(wxT("/SamplingRate/DefaultProjectSampleFormat"), floatSample);
for(chn = 0; chn < channels; chn++) {
data->channels[chn] = data->trackFactory->NewWaveTrack(format, samplerate).release();
data->channels[chn]->SetChannel(Track::MonoChannel);
for(auto &channel: data->channels) {
channel = data->trackFactory->NewWaveTrack(format, samplerate);
channel->SetChannel(Track::MonoChannel);
}
/* special case: 2 channels is understood to be stereo */
if(channels == 2) {
data->channels[0]->SetChannel(Track::LeftChannel);
data->channels[1]->SetChannel(Track::RightChannel);
data->channels[0]->SetLinked(true);
data->channels.begin()->get()->SetChannel(Track::LeftChannel);
data->channels.rbegin()->get()->SetChannel(Track::RightChannel);
data->channels.begin()->get()->SetLinked(true);
}
data->numChannels = channels;
}
@@ -513,19 +502,20 @@ enum mad_flow output_cb(void *_data,
* big blocks of data like this isn't a great idea, but it's temporary.
*/
float **channelBuffers = new float* [channels];
for(chn = 0; chn < channels; chn++)
for(int chn = 0; chn < channels; chn++)
channelBuffers[chn] = new float [samples];
for(smpl = 0; smpl < samples; smpl++)
for(chn = 0; chn < channels; chn++)
for(int chn = 0; chn < channels; chn++)
channelBuffers[chn][smpl] = scale(pcm->samples[chn][smpl]);
for(chn = 0; chn < channels; chn++)
data->channels[chn]->Append((samplePtr)channelBuffers[chn],
auto iter = data->channels.begin();
for (int chn = 0; chn < channels; ++iter, ++chn)
iter->get()->Append((samplePtr)channelBuffers[chn],
floatSample,
samples);
for(chn = 0; chn < channels; chn++)
for(int chn = 0; chn < channels; chn++)
delete[] channelBuffers[chn];
delete[] channelBuffers;

View File

@@ -124,8 +124,8 @@ public:
wxString GetFileDescription();
int GetFileUncompressedBytes();
int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags);
int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) override;
wxInt32 GetStreamCount()
{
@@ -155,7 +155,7 @@ private:
int *mStreamUsage;
wxArrayString *mStreamInfo;
WaveTrack ***mChannels;
std::list<TrackHolders> mChannels;
sampleFormat mFormat;
};
@@ -233,49 +233,55 @@ int OggImportFileHandle::GetFileUncompressedBytes()
return 0;
}
int OggImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags)
int OggImportFileHandle::Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
wxASSERT(mFile->IsOpened());
CreateProgress();
//Number of streams used may be less than mVorbisFile->links,
//but this way bitstream matches array index.
mChannels = new WaveTrack **[mVorbisFile->links];
mChannels.resize(mVorbisFile->links);
int i,c;
for (i = 0; i < mVorbisFile->links; i++)
int i = -1;
for (auto &link: mChannels)
{
++i;
//Stream is not used
if (mStreamUsage[i] == 0)
{
//This is just a padding to keep bitstream number and
//array indices matched.
mChannels[i] = NULL;
continue;
}
vorbis_info *vi = ov_info(mVorbisFile, i);
mChannels[i] = new WaveTrack *[vi->channels];
link.resize(vi->channels);
for (c = 0; c < vi->channels; c++) {
mChannels[i][c] = trackFactory->NewWaveTrack(mFormat, vi->rate).release();
int c = - 1;
for (auto &channel : link) {
++c;
channel = trackFactory->NewWaveTrack(mFormat, vi->rate);
if (vi->channels == 2) {
switch (c) {
case 0:
mChannels[i][c]->SetChannel(Track::LeftChannel);
mChannels[i][c]->SetLinked(true);
channel->SetChannel(Track::LeftChannel);
channel->SetLinked(true);
break;
case 1:
mChannels[i][c]->SetChannel(Track::RightChannel);
channel->SetChannel(Track::RightChannel);
break;
}
}
else {
mChannels[i][c]->SetChannel(Track::MonoChannel);
channel->SetChannel(Track::MonoChannel);
}
}
}
@@ -340,10 +346,13 @@ int OggImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
samplesRead = bytesRead / mVorbisFile->vi[bitstream].channels / sizeof(short);
/* give the data to the wavetracks */
auto iter = mChannels.begin();
std::advance(iter, bitstream);
if (mStreamUsage[bitstream] != 0)
{
for (c = 0; c < mVorbisFile->vi[bitstream].channels; c++)
mChannels[bitstream][c]->Append((char *)(mainBuffer + c),
auto iter2 = iter->begin();
for (int c = 0; c < mVorbisFile->vi[bitstream].channels; ++iter2, ++c)
iter2->get()->Append((char *)(mainBuffer + c),
int16Sample,
samplesRead,
mVorbisFile->vi[bitstream].channels);
@@ -365,48 +374,21 @@ int OggImportFileHandle::Import(TrackFactory *trackFactory, Track ***outTracks,
res = eProgressFailed;
if (res == eProgressFailed || res == eProgressCancelled) {
for (i = 0; i < mVorbisFile->links; i++)
{
if (mChannels[i])
{
for(c = 0; c < mVorbisFile->vi[bitstream].channels; c++) {
if (mChannels[i][c])
delete mChannels[i][c];
}
delete[] mChannels[i];
}
}
delete[] mChannels;
return res;
}
*outNumTracks = 0;
for (int s = 0; s < mVorbisFile->links; s++)
for (auto &link : mChannels)
{
if (mStreamUsage[s] != 0)
*outNumTracks += mVorbisFile->vi[s].channels;
}
*outTracks = new Track *[*outNumTracks];
int trackindex = 0;
for (i = 0; i < mVorbisFile->links; i++)
{
if (mChannels[i])
{
for (c = 0; c < mVorbisFile->vi[i].channels; c++) {
mChannels[i][c]->Flush();
(*outTracks)[trackindex++] = mChannels[i][c];
}
delete[] mChannels[i];
for (auto &channel : link) {
channel->Flush();
outTracks.push_back(std::move(channel));
}
}
delete[] mChannels;
//\todo { Extract comments from each stream? }
if (mVorbisFile->vc[0].comments > 0) {
tags->Clear();
for (c = 0; c < mVorbisFile->vc[0].comments; c++) {
for (int c = 0; c < mVorbisFile->vc[0].comments; c++) {
wxString comment = UTF8CTOWX(mVorbisFile->vc[0].user_comments[c]);
wxString name = comment.BeforeFirst(wxT('='));
wxString value = comment.AfterFirst(wxT('='));

View File

@@ -94,8 +94,8 @@ public:
wxString GetFileDescription();
int GetFileUncompressedBytes();
int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags);
int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) override;
wxInt32 GetStreamCount(){ return 1; }
@@ -318,10 +318,11 @@ How do you want to import the current file(s)?"), oldCopyPref == wxT("copy") ? _
}
int PCMImportFileHandle::Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
wxASSERT(mFile);
// Get the preference / warn the user about aliased files.
@@ -338,31 +339,31 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory,
CreateProgress();
WaveTrack **channels = new WaveTrack *[mInfo.channels];
TrackHolders channels(mInfo.channels);
int c;
for (c = 0; c < mInfo.channels; c++) {
channels[c] = trackFactory->NewWaveTrack(mFormat, mInfo.samplerate).release();
auto iter = channels.begin();
for (int c = 0; c < mInfo.channels; ++iter, ++c) {
*iter = trackFactory->NewWaveTrack(mFormat, mInfo.samplerate);
if (mInfo.channels > 1)
switch (c) {
case 0:
channels[c]->SetChannel(Track::LeftChannel);
iter->get()->SetChannel(Track::LeftChannel);
break;
case 1:
channels[c]->SetChannel(Track::RightChannel);
iter->get()->SetChannel(Track::RightChannel);
break;
default:
channels[c]->SetChannel(Track::MonoChannel);
iter->get()->SetChannel(Track::MonoChannel);
}
}
if (mInfo.channels == 2) {
channels[0]->SetLinked(true);
channels.begin()->get()->SetLinked(true);
}
sampleCount fileTotalFrames = (sampleCount)mInfo.frames;
sampleCount maxBlockSize = channels[0]->GetMaxBlockSize();
sampleCount maxBlockSize = channels.begin()->get()->GetMaxBlockSize();
int updateResult = false;
// If the format is not seekable, we must use 'copy' mode,
@@ -387,8 +388,9 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory,
if (i + blockLen > fileTotalFrames)
blockLen = fileTotalFrames - i;
for (c = 0; c < mInfo.channels; c++)
channels[c]->AppendAlias(mFilename, i, blockLen, c,useOD);
auto iter = channels.begin();
for (int c = 0; c < mInfo.channels; ++iter, ++c)
iter->get()->AppendAlias(mFilename, i, blockLen, c,useOD);
if (++updateCounter == 50) {
updateResult = mProgress->Update(i, fileTotalFrames);
@@ -403,9 +405,9 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory,
{
ODComputeSummaryTask* computeTask=new ODComputeSummaryTask;
bool moreThanStereo = mInfo.channels>2;
for (c = 0; c < mInfo.channels; c++)
for (const auto &channel : channels)
{
computeTask->AddWaveTrack(channels[c]);
computeTask->AddWaveTrack(channel.get());
if(moreThanStereo)
{
//if we have 3 more channels, they get imported on seperate tracks, so we add individual tasks for each.
@@ -454,7 +456,8 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory,
block = sf_readf_float(mFile, (float *)srcbuffer.ptr(), block);
if (block) {
for(c=0; c<mInfo.channels; c++) {
auto iter = channels.begin();
for(int c=0; c<mInfo.channels; ++iter, ++c) {
if (mFormat==int16Sample) {
for(int j=0; j<block; j++)
((short *)buffer.ptr())[j] =
@@ -466,7 +469,7 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory,
((float *)srcbuffer.ptr())[mInfo.channels*j+c];
}
channels[c]->Append(buffer.ptr(), (mFormat == int16Sample)?int16Sample:floatSample, block);
iter->get()->Append(buffer.ptr(), (mFormat == int16Sample)?int16Sample:floatSample, block);
}
framescompleted += block;
}
@@ -480,20 +483,13 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory,
}
if (updateResult == eProgressFailed || updateResult == eProgressCancelled) {
for (c = 0; c < mInfo.channels; c++)
delete channels[c];
delete[] channels;
return updateResult;
}
*outNumTracks = mInfo.channels;
*outTracks = new Track *[mInfo.channels];
for(c = 0; c < mInfo.channels; c++) {
channels[c]->Flush();
(*outTracks)[c] = channels[c];
}
delete[] channels;
for(const auto &channel : channels) {
channel->Flush();
}
outTracks.swap(channels);
const char *str;

View File

@@ -64,6 +64,8 @@ but little else.
#include "../widgets/ProgressDialog.h"
#include "ImportRaw.h" // defines TrackHolders
class TrackFactory;
class Track;
class Tags;
@@ -152,8 +154,8 @@ public:
// do the actual import, creating whatever tracks are necessary with
// the TrackFactory and calling the progress callback every iteration
// through the importing loop
virtual int Import(TrackFactory *trackFactory, Track ***outTracks,
int *outNumTracks, Tags *tags) = 0;
virtual int Import(TrackFactory *trackFactory, TrackHolders &outTracks,
Tags *tags) = 0;
// Return number of elements in stream list
virtual wxInt32 GetStreamCount() = 0;

View File

@@ -152,9 +152,8 @@ class QTImportFileHandle final : public ImportFileHandle
}
int Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
Tags *tags);
TrackHolders &outTracks,
Tags *tags) override;
private:
void AddMetadata(Tags *tags);
@@ -225,10 +224,11 @@ int QTImportFileHandle::GetFileUncompressedBytes()
}
int QTImportFileHandle::Import(TrackFactory *trackFactory,
Track ***outTracks,
int *outNumTracks,
TrackHolders &outTracks,
Tags *tags)
{
outTracks.clear();
OSErr err = noErr;
MovieAudioExtractionRef maer = NULL;
int updateResult = eProgressSuccess;
@@ -318,7 +318,7 @@ int QTImportFileHandle::Import(TrackFactory *trackFactory,
calloc(1, offsetof(AudioBufferList, mBuffers) + (sizeof(AudioBuffer) * numchan));
abl->mNumberBuffers = numchan;
WaveTrack **channels = new WaveTrack *[numchan];
TrackHolders channels{ numchan };
int c;
for (c = 0; c < numchan; c++) {
@@ -370,19 +370,11 @@ int QTImportFileHandle::Import(TrackFactory *trackFactory,
res = (updateResult == eProgressSuccess && err == noErr);
if (res) {
for (c = 0; c < numchan; c++) {
channels[c]->Flush();
for (const auto &channel: channels) {
channel->Flush();
}
*outTracks = (Track **) channels;
*outNumTracks = numchan;
}
else {
for (c = 0; c < numchan; c++) {
delete channels[c];
}
delete [] channels;
outTracks.swap(channels);
}
for (c = 0; c < numchan; c++) {

View File

@@ -93,9 +93,10 @@ class ImportRawDialog final : public wxDialog {
DECLARE_EVENT_TABLE()
};
int ImportRaw(wxWindow *parent, const wxString &fileName,
TrackFactory *trackFactory, Track ***outTracks)
void ImportRaw(wxWindow *parent, const wxString &fileName,
TrackFactory *trackFactory, TrackHolders &outTracks)
{
outTracks.clear();
int encoding = 0; // Guess Format
int numChannels = 0;
sampleFormat format;
@@ -130,7 +131,7 @@ int ImportRaw(wxWindow *parent, const wxString &fileName,
ImportRawDialog dlog(parent, encoding, numChannels, (int)offset, rate);
dlog.ShowModal();
if (!dlog.GetReturnCode())
return false;
return;
encoding = dlog.mEncoding;
numChannels = dlog.mChannels;
@@ -158,7 +159,7 @@ int ImportRaw(wxWindow *parent, const wxString &fileName,
sf_error_str((SNDFILE *)NULL, str, 1000);
printf("%s\n", str);
return 0;
return;
}
result = sf_command(sndFile, SFC_SET_RAW_START_OFFSET, &offset, sizeof(offset));
@@ -187,30 +188,32 @@ int ImportRaw(wxWindow *parent, const wxString &fileName,
sf_subtype_more_than_16_bits(encoding))
format = floatSample;
WaveTrack **channels = new WaveTrack *[numChannels];
TrackHolders channels(numChannels);
int c;
for (c = 0; c < numChannels; c++) {
channels[c] = trackFactory->NewWaveTrack(format, rate).release();
auto iter = channels.begin();
for (int c = 0; c < numChannels; ++iter, ++c) {
const auto channel =
(*iter = trackFactory->NewWaveTrack(format, rate)).get();
if (numChannels > 1)
switch (c) {
case 0:
channels[c]->SetChannel(Track::LeftChannel);
channel->SetChannel(Track::LeftChannel);
break;
case 1:
channels[c]->SetChannel(Track::RightChannel);
channel->SetChannel(Track::RightChannel);
break;
default:
channels[c]->SetChannel(Track::MonoChannel);
channel->SetChannel(Track::MonoChannel);
}
}
const auto firstChannel = channels.begin()->get();
if (numChannels == 2) {
channels[0]->SetLinked(true);
firstChannel->SetLinked(true);
}
sampleCount maxBlockSize = channels[0]->GetMaxBlockSize();
sampleCount maxBlockSize = firstChannel->GetMaxBlockSize();
int updateResult = eProgressSuccess;
SampleBuffer srcbuffer(maxBlockSize * numChannels, format);
@@ -238,7 +241,8 @@ int ImportRaw(wxWindow *parent, const wxString &fileName,
block = sf_readf_float(sndFile, (float *)srcbuffer.ptr(), block);
if (block) {
for(c=0; c<numChannels; c++) {
auto iter = channels.begin();
for(int c=0; c<numChannels; ++iter, ++c) {
if (format==int16Sample) {
for(int j=0; j<block; j++)
((short *)buffer.ptr())[j] =
@@ -250,7 +254,7 @@ int ImportRaw(wxWindow *parent, const wxString &fileName,
((float *)srcbuffer.ptr())[numChannels*j+c];
}
channels[c]->Append(buffer.ptr(), format, block);
iter->get()->Append(buffer.ptr(), format, block);
}
framescompleted += block;
}
@@ -269,19 +273,13 @@ int ImportRaw(wxWindow *parent, const wxString &fileName,
res = eProgressFailed;
if (res == eProgressFailed || res == eProgressCancelled) {
for (c = 0; c < numChannels; c++)
delete channels[c];
delete[] channels;
// It's a shame we can't return proper error code
return 0;
return;
}
for (c = 0; c < numChannels; c++)
channels[c]->Flush();
*outTracks = (Track **)channels;
return numChannels;
for (const auto &channel : channels)
channel->Flush();
outTracks.swap(channels);
}
//

View File

@@ -11,13 +11,40 @@
#ifndef __AUDACITY_IMPORT_RAW__
#define __AUDACITY_IMPORT_RAW__
#include "Import.h"
#include "../MemoryX.h"
class TrackFactory;
class WaveTrack;
class DirManager;
class wxString;
class wxWindow;
int ImportRaw(wxWindow *parent, const wxString &fileName,
TrackFactory *trackFactory, Track ***outTracks);
#include <vector>
#ifdef __AUDACITY_OLD_STD__
class TrackHolder : public std::shared_ptr < WaveTrack >
{
public:
// shared_ptr can construct from unique_ptr&& in newer std, but not older,
// so define it here
TrackHolder &operator=(std::unique_ptr<WaveTrack> &&that)
{
reset(that.release());
return *this;
}
};
using TrackHolders = std::vector<TrackHolder>;
#else
using TrackHolders = std::vector<std::unique_ptr<WaveTrack>>;
#endif
void ImportRaw(wxWindow *parent, const wxString &fileName,
TrackFactory *trackFactory, TrackHolders &outTracks);
#endif