summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.5.5/player/Audio_Scope.cpp
blob: 464392d507d0ae0ce5d354259b675fba1dafdf24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Game_Music_Emu 0.5.5. http://www.slack.net/~ant/

#include "Audio_Scope.h"

#include <assert.h>
#include <stdlib.h>

/* Copyright (C) 2005-2006 by Shay Green. Permission is hereby granted, free of
charge, to any person obtaining a copy of this software module and associated
documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the
following conditions: The above copyright notice and this permission notice
shall be included in all copies or substantial portions of the Software. THE
SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

int const step_bits = 8;
int const step_unit = 1 << step_bits;
int const erase_color = 1;
int const draw_color = 2;

Audio_Scope::Audio_Scope()
{
	surface = 0;
	buf = 0;
}

Audio_Scope::~Audio_Scope()
{
	free( buf );
	
	if ( surface )
		SDL_FreeSurface( surface );
}

const char* Audio_Scope::init( int width, int height )
{
	assert( height <= 256 );
	assert( !buf ); // can only call init() once
	
	buf = (byte*) calloc( width * sizeof *buf, 1 );
	if ( !buf )
		return "Out of memory";
	
	low_y = 0;
	high_y = height;
	buf_size = width;
	
	for ( sample_shift = 6; sample_shift < 14; )
		if ( ((0x7FFFL * 2) >> sample_shift++) < height )
			break;
	
	v_offset = height / 2 - (0x10000 >> sample_shift);
	
	screen = SDL_SetVideoMode( width, height, 0, 0 );
	if ( !screen )
		return "Couldn't set video mode";
	
	surface = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0 );
	if ( !screen )
		return "Couldn't create surface";
	
	static SDL_Color palette [2] = { {0, 0, 0}, {0, 255, 0} };
	SDL_SetColors( surface, palette, 1, 2 );
	
	return 0; // success
}

const char* Audio_Scope::draw( const short* in, long count, double step )
{
	int low = low_y;
	int high = high_y;
	
	if ( count >= buf_size )
	{
		count = buf_size;
		low_y = 0x7FFF;
		high_y = 0;
	}
	
	if ( SDL_LockSurface( surface ) < 0 )
		return "Couldn't lock surface";
	render( in, count, (long) (step * step_unit) );
	SDL_UnlockSurface( surface );
	
	if ( low > low_y )
		low = low_y;
	
	if ( high < high_y )
		high = high_y;
	
	SDL_Rect r;
	r.x = 0;
	r.w = buf_size;
	r.y = low + v_offset;
	r.h = high - low + 1;
	
	if ( SDL_BlitSurface( surface, &r, screen, &r ) < 0 )
		return "Blit to screen failed";
	
	if ( SDL_Flip( screen ) < 0 )
		return "Couldn't flip screen";
	
	return 0; // success
}

void Audio_Scope::render( short const* in, long count, long step )
{
	byte* old_pos = buf;
	long surface_pitch = surface->pitch;
	byte* out = (byte*) surface->pixels + v_offset * surface_pitch;
	int old_erase = *old_pos;
	int old_draw = 0;
	long in_pos = 0;
	
	int low_y  = this->low_y;
	int high_y = this->high_y;
	int half_step = (step + step_unit / 2) >> (step_bits + 1);
	
	while ( count-- )
	{
		// Line drawing/erasing starts at previous sample and ends one short of
		// current sample, except when previous and current are the same.
		
		// Extra read on the last iteration of line loops will always be at the
		// height of the next sample, and thus within the gworld bounds.
		
		// Erase old line
		{
			int delta = *old_pos - old_erase;
			int offset = old_erase * surface_pitch;
			old_erase += delta;
			
			int next_line = surface_pitch;
			if ( delta < 0 )
			{
				delta = -delta;
				next_line = -surface_pitch;
			}
			
			do
			{
				out [offset] = erase_color;
				offset += next_line;
			}
			while ( delta-- > 1 );
		}
		
		// Draw new line and put in old_buf
		{
			
			int in_whole = in_pos >> step_bits;
			int sample = (0x7FFF * 2 - in [in_whole] - in [in_whole + half_step]) >> sample_shift;
			if ( !in_pos )
				old_draw = sample;
			in_pos += step;
			
			int delta = sample - old_draw;
			int offset = old_draw * surface_pitch;
			old_draw += delta;
			
			int next_line = surface_pitch;
			if ( delta < 0 )
			{
				delta = -delta;
				next_line = -surface_pitch;
			}
			
			*old_pos++ = sample;
			
			// min/max updating can be interleved anywhere
			
			if ( low_y > sample )
				low_y = sample;
			
			do
			{
				out [offset] = draw_color;
				offset += next_line;
			}
			while ( delta-- > 1 );
			
			if ( high_y < sample )
				high_y = sample;
		}
		
		out++;
	}
	
	this->low_y = low_y;
	this->high_y = high_y;
}