Merge with trunk, revision 28528 - 28976.
[blender-staging.git] / intern / audaspace / intern / AUD_FileFactory.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * AudaSpace is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #include "AUD_FileFactory.h"
27 #include "AUD_Buffer.h"
28
29 #include <cstring>
30
31 #ifdef WITH_FFMPEG
32 // needed for INT64_C
33 #define __STDC_CONSTANT_MACROS
34
35 #include "AUD_FFMPEGReader.h"
36 #endif
37 #ifdef WITH_SNDFILE
38 #include "AUD_SndFileReader.h"
39 #endif
40
41 AUD_FileFactory::AUD_FileFactory(const char* filename)
42 {
43         if(filename != NULL)
44         {
45                 m_filename = new char[strlen(filename)+1]; AUD_NEW("string")
46                 strcpy(m_filename, filename);
47         }
48         else
49                 m_filename = NULL;
50 }
51
52 AUD_FileFactory::AUD_FileFactory(unsigned char* buffer, int size)
53 {
54         m_filename = NULL;
55         m_buffer = AUD_Reference<AUD_Buffer>(new AUD_Buffer(size));
56         memcpy(m_buffer.get()->getBuffer(), buffer, size);
57 }
58
59 AUD_FileFactory::~AUD_FileFactory()
60 {
61         if(m_filename)
62         {
63                 delete[] m_filename; AUD_DELETE("string")
64         }
65 }
66
67 AUD_IReader* AUD_FileFactory::createReader()
68 {
69         AUD_IReader* reader = 0;
70
71 #ifdef WITH_SNDFILE
72         try
73         {
74                 if(m_filename)
75                         reader = new AUD_SndFileReader(m_filename);
76                 else
77                         reader = new AUD_SndFileReader(m_buffer);
78                 AUD_NEW("reader")
79                 return reader;
80         }
81         catch(AUD_Exception e) {}
82 #endif
83
84 #ifdef WITH_FFMPEG
85         try
86         {
87                 if(m_filename)
88                         reader = new AUD_FFMPEGReader(m_filename);
89                 else
90                         reader = new AUD_FFMPEGReader(m_buffer);
91                 AUD_NEW("reader")
92                 return reader;
93         }
94         catch(AUD_Exception e) {}
95 #endif
96
97         return reader;
98 }