Mercurial > repos > blastem
comparison ym2612.c @ 884:252dfd29831d
Selecting a second game from the menu now works
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 13 Nov 2015 22:56:59 -0800 |
parents | 46bb673eed4e |
children | 0ee8cfcc06d1 |
comparison
equal
deleted
inserted
replaced
883:9f149f0e98b7 | 884:252dfd29831d |
---|---|
102 | 102 |
103 ym2612_context * log_context = NULL; | 103 ym2612_context * log_context = NULL; |
104 | 104 |
105 void ym_finalize_log() | 105 void ym_finalize_log() |
106 { | 106 { |
107 if (!log_context) { | |
108 return; | |
109 } | |
107 for (int i = 0; i < NUM_CHANNELS; i++) { | 110 for (int i = 0; i < NUM_CHANNELS; i++) { |
108 if (log_context->channels[i].logfile) { | 111 if (log_context->channels[i].logfile) { |
109 wave_finalize(log_context->channels[i].logfile); | 112 wave_finalize(log_context->channels[i].logfile); |
110 } | 113 } |
111 } | 114 } |
115 log_context = NULL; | |
112 } | 116 } |
113 #define BUFFER_INC_RES 1000000000UL | 117 #define BUFFER_INC_RES 1000000000UL |
114 | 118 |
115 void ym_adjust_master_clock(ym2612_context * context, uint32_t master_clock) | 119 void ym_adjust_master_clock(ym2612_context * context, uint32_t master_clock) |
116 { | 120 { |
122 #define log2(x) (log(x)/log(2)) | 126 #define log2(x) (log(x)/log(2)) |
123 #endif | 127 #endif |
124 | 128 |
125 void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options) | 129 void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options) |
126 { | 130 { |
131 static uint8_t registered_finalize; | |
127 dfopen(debug_file, "ym_debug.txt", "w"); | 132 dfopen(debug_file, "ym_debug.txt", "w"); |
128 memset(context, 0, sizeof(*context)); | 133 memset(context, 0, sizeof(*context)); |
129 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * sample_limit*2); | 134 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * sample_limit*2); |
130 context->back_buffer = malloc(sizeof(*context->audio_buffer) * sample_limit*2); | 135 context->back_buffer = malloc(sizeof(*context->audio_buffer) * sample_limit*2); |
131 context->sample_rate = sample_rate; | 136 context->sample_rate = sample_rate; |
155 } | 160 } |
156 } | 161 } |
157 } | 162 } |
158 if (options & YM_OPT_WAVE_LOG) { | 163 if (options & YM_OPT_WAVE_LOG) { |
159 log_context = context; | 164 log_context = context; |
160 atexit(ym_finalize_log); | 165 if (!registered_finalize) { |
166 atexit(ym_finalize_log); | |
167 registered_finalize = 1; | |
168 } | |
161 } | 169 } |
162 if (!did_tbl_init) { | 170 if (!did_tbl_init) { |
163 //populate sine table | 171 //populate sine table |
164 for (int32_t i = 0; i < 512; i++) { | 172 for (int32_t i = 0; i < 512; i++) { |
165 double sine = sin( ((double)(i*2+1) / SINE_TABLE_SIZE) * M_PI_2 ); | 173 double sine = sin( ((double)(i*2+1) / SINE_TABLE_SIZE) * M_PI_2 ); |
214 lfo_pm_table[freq * 256 + pms * 32 + step] = value; | 222 lfo_pm_table[freq * 256 + pms * 32 + step] = value; |
215 } | 223 } |
216 } | 224 } |
217 } | 225 } |
218 } | 226 } |
227 } | |
228 | |
229 void ym_free(ym2612_context *context) | |
230 { | |
231 if (context == log_context) { | |
232 ym_finalize_log(); | |
233 } | |
234 free(context->audio_buffer); | |
235 //TODO: Figure out how to make this 100% safe | |
236 //audio thread could still be using this | |
237 free(context->back_buffer); | |
238 free(context); | |
219 } | 239 } |
220 | 240 |
221 #define YM_VOLUME_MULTIPLIER 2 | 241 #define YM_VOLUME_MULTIPLIER 2 |
222 #define YM_VOLUME_DIVIDER 3 | 242 #define YM_VOLUME_DIVIDER 3 |
223 #define YM_MOD_SHIFT 1 | 243 #define YM_MOD_SHIFT 1 |