1 /*
2  * Copyright (c) 2009, The MilkyTracker Team.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * - Neither the name of the <ORGANIZATION> nor the names of its contributors
14  *   may be used to endorse or promote products derived from this software
15  *   without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  *  SampleLoaderGeneric.cpp
32  *  MilkyPlay
33  *
34  *  Created by Peter Barth on 16.09.05.
35  *
36  */
37 
38 #include "SampleLoaderGeneric.h"
39 #include "SampleLoaderWAV.h"
40 #include "SampleLoaderIFF.h"
41 #include "SampleLoaderAIFF.h"
42 #include "SampleLoaderALL.h"
43 
SampleLoaderGeneric(const SYSCHAR * fileName,XModule & module,bool supportRawLoading)44 SampleLoaderGeneric::SampleLoaderGeneric(const SYSCHAR* fileName, XModule& module, bool supportRawLoading/* = true*/)
45 	: SampleLoaderAbstract(fileName, module)
46 	, supportRawLoading(supportRawLoading)
47 {
48 }
49 
getNumChannels()50 mp_sint32 SampleLoaderGeneric::getNumChannels()
51 {
52 	SampleLoaderAbstract* loader = getSuitableLoader();
53 
54 	if (loader)
55 	{
56 		mp_sint32 res = loader->getNumChannels();
57 		delete loader;
58 		return res;
59 	}
60 
61 	return 0;
62 }
63 
getChannelName(mp_sint32 channelIndex)64 const char* SampleLoaderGeneric::getChannelName(mp_sint32 channelIndex)
65 {
66 	SampleLoaderAbstract* loader = getSuitableLoader();
67 
68 	if (loader)
69 	{
70 		const char* res = loader->getChannelName(channelIndex);
71 		delete loader;
72 		return res;
73 	}
74 
75 	return SampleLoaderAbstract::getChannelName(channelIndex);
76 }
77 
identifySample()78 bool SampleLoaderGeneric::identifySample()
79 {
80 	SampleLoaderAbstract* loader = getSuitableLoader();
81 
82 	if (loader)
83 	{
84 		delete loader;
85 		return true;
86 	}
87 
88 	return false;
89 }
90 
loadSample(mp_sint32 index,mp_sint32 channelIndex)91 mp_sint32 SampleLoaderGeneric::loadSample(mp_sint32 index, mp_sint32 channelIndex)
92 {
93 	SampleLoaderAbstract* loader = getSuitableLoader();
94 
95 	if (loader)
96 	{
97 		loader->setPreferredDefaultName(this->preferredDefaultName);
98 
99 		mp_sint32 res = loader->loadSample(index, channelIndex);
100 		delete loader;
101 		return res;
102 	}
103 
104 	return MP_UNSUPPORTED_FORMAT;
105 }
106 
saveSample(const SYSCHAR * fileName,mp_sint32 index,OutputFiletypes type)107 mp_sint32 SampleLoaderGeneric::saveSample(const SYSCHAR* fileName, mp_sint32 index, OutputFiletypes type)
108 {
109 	SampleLoaderAbstract* loader = NULL;
110 
111 	switch (type)
112 	{
113 		case OutputFiletypeWAV:
114 			loader = new SampleLoaderWAV(theFileName, theModule);
115 			break;
116 
117 		case OutputFiletypeIFF:
118 			loader = new SampleLoaderIFF(theFileName, theModule);
119 			break;
120 
121 		case OutputFiletypeAIFF:
122 			loader = new SampleLoaderAIFF(theFileName, theModule);
123 			break;
124 
125 		case OutputFiletypeRAW:
126 			if (!supportRawLoading)
127 				break;
128 			loader = new SampleLoaderALL(theFileName, theModule);
129 			break;
130 	}
131 
132 	if (loader)
133 	{
134 		mp_sint32 res = loader->saveSample(fileName, index);
135 		delete loader;
136 		return res;
137 	}
138 
139 	return MP_UNSUPPORTED_FORMAT;
140 }
141 
getSuitableLoader()142 SampleLoaderAbstract* SampleLoaderGeneric::getSuitableLoader()
143 {
144 	// Try to find WAV first
145 	SampleLoaderAbstract* loader = new SampleLoaderWAV(theFileName, theModule);
146 	if (loader && loader->identifySample())
147 		return loader;
148 
149 	delete loader;
150 
151 	// Try to find IFF then
152 	loader = new SampleLoaderIFF(theFileName, theModule);
153 	if (loader && loader->identifySample())
154 		return loader;
155 
156 	delete loader;
157 
158 	// Try to find AIFF then
159 	loader = new SampleLoaderAIFF(theFileName, theModule);
160 	if (loader && loader->identifySample())
161 		return loader;
162 
163 	delete loader;
164 
165 	if (supportRawLoading)
166 	{
167 		// Try to find something else
168 		loader = new SampleLoaderALL(theFileName, theModule);
169 		if (loader && loader->identifySample())
170 			return loader;
171 
172 		delete loader;
173 	}
174 
175 	return NULL;
176 }
177