1 /* $Id: notes.c,v 1.5 2006/09/15 15:13:19 toad32767 Exp $ */
2 /**
3  ** 2005, 2006 by Marco Trillo
4  ** This file is part of UModPlayer, and is released by
5  ** its autors to the Public Domain.
6  ** In case it's not legally possible, its autors grant
7  ** anyone the right to use, redistribute and modify
8  ** this software for any purpose, without any conditions,
9  ** unless such conditions are required by law.
10  **
11  ** THIS FILE COMES WITHOUT ANY WARRANTY. THE AUTHORS
12  ** SHALL NOT BE LIABLE FOR ANY DAMAGE RESULTING BY THE
13  ** USE OR MISUSE OF THIS SOFTWARE.
14  **/
15 
16 /*
17  * =====================
18  *     NOTE DRAWER
19  * =====================
20  */
21 
22 #include <umodplayer.h>
23 #include <coresound.h>
24 
25 LOCAL int first_channel;
26 LOCAL int last_channel;
27 
28 #define PUTSTR(strd,echar) strd[0]=strd[1]=strd[2]=(echar)
29 
30 LOCAL char *Notes[12] = {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"};
31 
32 LOCAL void
NoteGetName(int note,char * string)33 NoteGetName(int note, char *string)
34 {
35 	int realnote, octave;
36 
37 	if (note == 0)
38 		PUTSTR(string, '.');	/* no note  (...)  */
39 	else if (note == 254)
40 		PUTSTR(string, '^');	/* note cut (^^^)  */
41 	else if (note == 255)
42 		PUTSTR(string, '=');	/* note off (===)  */
43 	else if (note == 253)
44 		PUTSTR(string, '~');	/* note fade(~~~)  */
45 	else {			/* normal note */
46 		realnote = note - 1;
47 		octave = realnote / 12;
48 		realnote %= 12;
49 
50 		strcpy(string, Notes[realnote]);	/* put note name */
51 		sprintf(&string[2], "%1d", octave);	/* put octave    */
52 
53 	}
54 	string[3] = '\0';	/* finish string */
55 	return;
56 }
57 #undef PUTSTR
58 
59 LOCAL void
DrawHead()60 DrawHead()
61 {
62 	int i;
63 
64 	fputs("row", stdout);
65 
66 	for (i = first_channel; i <= last_channel; ++i) {
67 		printf(" %3d", i);
68 	}
69 	putchar('\n');
70 
71 	for (i = (first_channel - 1); i <= last_channel; ++i) {
72 		fputs("----", stdout);
73 	}
74 	putchar('\n');
75 }
76 
77 /* monitor function for CoreSound */
78 EXPORT void
DrawSong()79 DrawSong()
80 {
81 	int pattern, row, oldrow = -1, oldpattern = 0;
82 	int tics = 0;
83 	int channels;
84 	char noteName[4];
85 	int i;
86 	ModPlugNote *pat;
87 
88 	for (;;) {
89 
90 		/*
91 		 * Inject the audio samples
92 		 */
93 		CoreSound_InjectPCM();
94 
95 		if (AudioActive == FALSE || KeyPress()) {
96 			CoreSound_Quit();
97 			return;
98 		}
99 		pattern = ModPlug_GetCurrentPattern(file.mod);
100 		row = ModPlug_GetCurrentRow(file.mod);
101 
102 		if (oldpattern != pattern) {
103 			puts("\n\n\n");
104 
105 			if ((++tics) == 4) {
106 				DrawHead();
107 				tics = 0;
108 			}
109 		}
110 		if (oldrow != row) {
111 			/* Draw the row */
112 			pat = ModPlug_GetPattern(file.mod, pattern, NULL);
113 			channels = (int) ModPlug_NumChannels(file.mod);
114 
115 			pat += channels * row + first_channel - 1;	/* go to current row */
116 			if (!pat) {
117 				CoreSound_Quit();
118 				return;
119 			}
120 
121 			printf("%3d", row);
122 			for (i = first_channel; i <= last_channel; ++i) {
123 				NoteGetName((int) (pat->Note), noteName);
124 				printf(" %s", noteName);
125 				++pat;
126 			}
127 			putchar('\n');
128 
129 		}
130 		oldrow = row;
131 		oldpattern = pattern;
132 	}
133 	return;
134 }
135 
136 EXPORT void
NoteDisplay_Start(int x,int y)137 NoteDisplay_Start(int x, int y)
138 {
139 	int channels = (int) ModPlug_NumChannels(file.mod);
140 
141 	if (x < 0)
142 		first_channel = 1;
143 	else {
144 		if (x == 0 || x > channels)
145 			first_channel = 1;
146 		else
147 			first_channel = x;
148 	}
149 
150 	if (y < 0)
151 		last_channel = channels;
152 	else {
153 		if (y < 1 || y > channels)
154 			last_channel = channels;
155 		else
156 			last_channel = y;
157 	}
158 
159 	if (first_channel > last_channel)
160 		first_channel = last_channel;
161 
162 	DrawHead();
163 	CoreSound_Start(DrawSong);
164 	return;
165 }
166 
167