1919
2020#include " audio_processor.h"
2121#include " codec/audio_vorbis.h"
22+ #include " codec/audio_qoa.h"
2223
2324#include < SDL2/SDL.h>
2425
@@ -71,6 +72,9 @@ const MDAudioFileSpec &MoondustAudioProcessor::getInSpec() const
7172
7273bool MoondustAudioProcessor::openInFile (const std::string &file, int *detectedFormat)
7374{
75+ Uint8 magic[100 ];
76+ Uint8 submagic[4 ];
77+
7478 if (m_rw_in)
7579 SDL_RWclose (m_rw_in);
7680
@@ -82,16 +86,60 @@ bool MoondustAudioProcessor::openInFile(const std::string &file, int *detectedFo
8286 return false ;
8387 }
8488
89+ SDL_memset (magic, 0 , 100 );
90+ if (SDL_RWread (m_rw_in, magic, 1 , 99 ) < 24 )
91+ {
92+ m_lastError = " Couldn't read any first 24 bytes of audio data" ;
93+ m_in_file.reset ();
94+ SDL_RWclose (m_rw_in);
95+ m_rw_in = nullptr ;
96+ return false ;
97+ }
98+ SDL_RWseek (m_rw_in, 0 , RW_SEEK_SET );
99+
85100 m_in_filePath = file;
101+ m_in_file.reset ();
102+
103+ if (SDL_memcmp (magic, " OggS" , 4 ) == 0 )
104+ {
105+ SDL_RWseek (m_rw_in, 28 , RW_SEEK_CUR );
106+ SDL_RWread (m_rw_in, magic, 1 , 8 );
107+ SDL_RWseek (m_rw_in,-36 , RW_SEEK_CUR );
86108
109+ if (SDL_memcmp (magic, " OpusHead" , 8 ) == 0 )
110+ {
111+ // m_in_file.reset(new MDAudioOpus);
112+ }
113+ else if (magic[0 ] == 0x7F && SDL_memcmp (magic + 1 , " FLAC" , 4 ) == 0 )
114+ {
115+ // m_in_file.reset(new MDAudioFLAC);
116+ }
117+ else
118+ {
119+ m_in_file.reset (new MDAudioVorbis);
120+ }
121+ }
122+ else if (SDL_memcmp (magic, " qoaf" , 4 ) == 0 )
123+ m_in_file.reset (new MDAudioQOA);
124+ else if (SDL_memcmp (magic, " XQOA" , 4 ) == 0 )
125+ m_in_file.reset (new MDAudioQOA (true ));
87126
88- m_in_file. reset ( new MDAudioVorbis );
127+ SDL_RWseek (m_rw_in, 0 , RW_SEEK_SET );
89128
129+ if (!m_in_file.get ())
130+ {
131+ m_lastError = " Unknown or unsupported file format" ;
132+ SDL_RWclose (m_rw_in);
133+ m_rw_in = nullptr ;
134+ return false ;
135+ }
90136
91137 if (!m_in_file->openRead (m_rw_in))
92138 {
93139 m_lastError = m_in_file->getLastError ();
94140 m_in_file.reset ();
141+ SDL_RWclose (m_rw_in);
142+ m_rw_in = nullptr ;
95143 return false ;
96144 }
97145
@@ -115,6 +163,12 @@ bool MoondustAudioProcessor::openOutFile(const std::string &file, int dstFormat,
115163 case FORMAT_OGG_VORBIS :
116164 m_out_file.reset (new MDAudioVorbis);
117165 break ;
166+ case FORMAT_QOA :
167+ m_out_file.reset (new MDAudioQOA);
168+ break ;
169+ case FORMAT_XQOA :
170+ m_out_file.reset (new MDAudioQOA (true ));
171+ break ;
118172 default :
119173 m_lastError = " Incorrect or unsupported destination format" ;
120174 return false ;
0 commit comments