1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3 
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #include "cydentry.h"
27 #include "cyddefs.h"
28 #include "freqs.h"
29 #include "SDL_endian.h"
30 #include <stdlib.h>
31 #include <string.h>
32 
33 
cyd_wave_entry_deinit(CydWavetableEntry * entry)34 void cyd_wave_entry_deinit(CydWavetableEntry *entry)
35 {
36 	if (entry->data) free(entry->data);
37 	entry->data = NULL;
38 }
39 
40 
cyd_wave_entry_init(CydWavetableEntry * entry,const void * data,Uint32 n_samples,CydWaveType sample_type,int channels,int denom,int nom)41 void cyd_wave_entry_init(CydWavetableEntry *entry, const void *data, Uint32 n_samples, CydWaveType sample_type, int channels, int denom, int nom)
42 {
43 	if (data && n_samples > 0)
44 	{
45 		entry->data = realloc(entry->data, sizeof(*entry->data) * n_samples);
46 
47 		for (int i = 0; i < n_samples ; ++i)
48 		{
49 			Sint32 v = 0;
50 
51 			for (int c = 0; c < channels ; ++c)
52 			{
53 				switch (sample_type)
54 				{
55 					case CYD_WAVE_TYPE_SINT16:
56 						v += SDL_SwapLE16(((Sint16*)data)[i * channels + c]);
57 						break;
58 
59 					case CYD_WAVE_TYPE_UINT8:
60 						v += ((Sint16)(((Uint8*)data)[i * channels + c]) - 0x80) << 8;
61 						break;
62 
63 					case CYD_WAVE_TYPE_SINT8:
64 						v += (Sint16)(((Sint8*)data)[i * channels + c]) << 8;
65 						break;
66 				}
67 			}
68 
69 			if (channels > 1)
70 				v /= channels;
71 
72 			entry->data[i] = v * denom / nom;
73 		}
74 
75 		entry->samples = n_samples;
76 	}
77 	else
78 	{
79 		free(entry->data);
80 		entry->data = NULL;
81 		entry->samples = 0;
82 	}
83 }
84