···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+#include "AVFormatFileObject.h"
2020+#include <sstream>
2121+#include "AudioFileFormatMP3.h"
2222+2323+extern "C" {
2424+#include <libavformat/avformat.h>
2525+}
2626+2727+#pragma GCC visibility push(default)
2828+AUDIOCOMPONENT_ENTRY(AudioFileComponentFactory, AVFormatFileObject);
2929+#pragma GCC visibility pop
3030+3131+AVFormatFileObject::AVFormatFileObject(AudioComponentInstance inInstance)
3232+: AudioFileObjectComponentBase(inInstance)
3333+{
3434+3535+}
3636+3737+// Analyze mAudioFileObject and return an AudioFileFormat based on that
3838+AudioFileFormat* AVFormatFileObject::GetAudioFormat() const
3939+{
4040+ uint8_t buffer[1024 + AVPROBE_PADDING_SIZE];
4141+ AVProbeData probeData;
4242+4343+ probeData.filename = "";
4444+ probeData.buf = buffer;
4545+ probeData.buf_size = 1024;
4646+4747+ if (mAudioFileObject->ReadBytes(false, 0, (UInt32*)&probeData.buf_size, probeData.buf) != noErr)
4848+ return nullptr;
4949+5050+ AVInputFormat* fmt = av_probe_input_format(&probeData, true);
5151+ if (!fmt)
5252+ return nullptr;
5353+5454+ std::istringstream ss(fmt->name);
5555+ std::string formatName;
5656+5757+ while (std::getline(ss, formatName, ','))
5858+ {
5959+ if (formatName == "aac") // Raw AAC
6060+ {
6161+6262+ }
6363+ else if (formatName == "ac3")
6464+ {
6565+6666+ }
6767+ else if (formatName == "aiff")
6868+ {
6969+7070+ }
7171+ else if (formatName == "caff")
7272+ {
7373+7474+ }
7575+ else if (formatName == "mp3")
7676+ {
7777+ return AudioFileFormatMP3::instance();
7878+ }
7979+ else if (formatName == "mov" || formatName == "mp4")
8080+ {
8181+8282+ }
8383+ else if (formatName == "au")
8484+ {
8585+8686+ }
8787+ else if (formatName == "wav")
8888+ {
8989+9090+ }
9191+ }
9292+9393+ return nullptr;
9494+}
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#ifndef _AV_FORMAT_FILE_OBJECT_H
2121+#define _AV_FORMAT_FILE_OBJECT_H
2222+#include "AudioFileComponentBase.h"
2323+2424+class AVFormatFileObject : public AudioFileObjectComponentBase
2525+{
2626+public:
2727+ AVFormatFileObject(AudioComponentInstance inInstance);
2828+ AudioFileFormat* GetAudioFormat() const override;
2929+};
3030+3131+#endif
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#ifndef _AUDIO_FILE_FORMAT_GENERIC_H
2121+#define _AUDIO_FILE_FORMAT_GENERIC_H
2222+#include "AudioFileFormat.h"
2323+2424+class AudioFileFormatGeneric : public AudioFileFormat
2525+{
2626+public:
2727+ AudioFileFormatGeneric(UInt32 inFileType, const char* avformatShortName);
2828+ Boolean ExtensionIsThisFormat(CFStringRef inExtension) override;
2929+ void GetExtensions(CFArrayRef *outArray) override;
3030+ void GetUTIs(CFArrayRef *outArray) override;
3131+ void GetMIMETypes(CFArrayRef *outArray) override;
3232+ void GetFileTypeName(CFStringRef *outName) override;
3333+ UncertainResult FileDataIsThisFormat(UInt32 inDataByteSize, const void* inData) override;
3434+3535+ AudioFileObject* New() override;
3636+ AudioFileStreamObject* NewStream() override { return NULL; }
3737+ OSStatus GetAvailableFormatIDs(UInt32* ioDataSize, void* outPropertyData) override;
3838+ OSStatus GetAvailableStreamDescriptions(UInt32 inFormatID, UInt32* ioDataSize, void* outPropertyData) override;
3939+private:
4040+ static CFArrayRef toCFArray(const std::vector<const char*>& array);
4141+protected:
4242+ struct Description
4343+ {
4444+ const char* name;
4545+ std::vector<const char*> extensions;
4646+ std::vector<const char*> utis;
4747+ std::vector<const char*> mimeTypes;
4848+ };
4949+ virtual const Description& description() const = 0;
5050+private:
5151+ const char* m_avformatShortName;
5252+};
5353+5454+#endif
5555+
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#include "AudioFileFormatMP3.h"
2121+2222+AudioFileFormatMP3::AudioFileFormatMP3()
2323+: AudioFileFormatGeneric('.mp3', "mp3")
2424+{
2525+}
2626+2727+AudioFileFormatMP3* AudioFileFormatMP3::instance()
2828+{
2929+ static AudioFileFormatMP3 inst;
3030+ return &inst;
3131+}
3232+3333+const AudioFileFormatMP3::Description& AudioFileFormatMP3::description() const
3434+{
3535+ static const Description d = {
3636+ .name = "MPEG Layer 3",
3737+ .extensions = { "mp3" },
3838+ .utis = { "public.mp3", "public.audio", "public.data" },
3939+ .mimeTypes = { "audio/mpeg" },
4040+ };
4141+ return d;
4242+}
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2020 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#ifndef _AUDIO_FILE_FORMAT_MP3_H
2121+#define _AUDIO_FILE_FORMAT_MP3_H
2222+#include "AudioFileFormatGeneric.h"
2323+2424+class AudioFileFormatMP3 : public AudioFileFormatGeneric
2525+{
2626+private:
2727+ AudioFileFormatMP3();
2828+public:
2929+ static AudioFileFormatMP3* instance();
3030+protected:
3131+ const Description& description() const override;
3232+};
3333+3434+#endif
3535+
···11+This is a file format component built on top of AVFormat.
22+33+The area of macOS surrounding AudioFileComponents is very badly documented, especially with regard to where this component belongs etc.
44+This project may serve as an example: https://sourceforge.net/projects/coreaudio-flac/
55+66+The goal is to support formats specified here: https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/CoreAudioOverview/SupportedAudioFormatsMacOSX/SupportedAudioFormatsMacOSX.html