File: analysis_highlighting.c

package info (click to toggle)
denemo 0.5.9-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,500 kB
  • ctags: 2,415
  • sloc: ansic: 23,057; sh: 3,321; yacc: 1,737; makefile: 449; lex: 376
file content (281 lines) | stat: -rw-r--r-- 6,193 bytes parent folder | download
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * analysis_highlighting.c
 * Implements note highlighting for 
 * analysis results
 *
 * A Tee (c) 2001
 */

#include "datastructures.h"
#include "analysis_highlighting.h"
#include "frogio.h"
#include "utils.h"
#include "prefops.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#ifndef G_OS_WIN32
# include <wait.h>
#endif
#include <errno.h>



GList *results = NULL;

void
unhighlight (gpointer callback_data, GtkWidget * widget)
{
  staffnode *curstaff;
  staff *curstaffstruct;
  measurenode *curmeasure;
  objnode *curobj;
  mudelaobject *mudelaitem;
  struct scoreinfo *si = callback_data;
  
  for (curstaff = si->thescore; curstaff; curstaff = curstaff->next)
    {
      curstaffstruct = curstaff->data;
      for (curmeasure = (measurenode *) curstaffstruct->measures;
	   curmeasure; curmeasure = curmeasure->next)
	{

	  for (curobj = curmeasure->data; curobj; curobj = curobj->next)
	    {
	      mudelaitem = curobj->data;
	      mudelaitem->u.chordval.is_highlighted = FALSE;
	    }
	}
    }
  //  results = NULL;  
}


struct callbackdata
{
  struct scoreinfo *si;
  GtkWidget *nextbutton;
  GtkWidget *prevbutton;
};


void
highlight (gpointer callback_data, GtkWidget * widget)
{
  struct scoreinfo *si = callback_data;
  note_highlight (si, widget);
}


void
note_highlight (gpointer callback_data, GtkWidget * widget)
{
  staffnode *curstaff = NULL;
  staff *curstaffstruct = NULL;
  measurenode *curmeasure = NULL;
  objnode *curobj = NULL;
  mudelaobject *mudelaitem = NULL;
  Results *res = NULL;
  gint bar;
  gint i;
  gfloat beat;
  static gint counter = 0;
  GList *temp;

  /*struct callbackdata *cbdata = callback_data; */
  struct scoreinfo *si = callback_data;

  /*if(!results) */
  read_resultsfile (si, widget);

  /*res = (Results *) g_list_nth_data(results,counter); */
  /*unhighlight(cbdata->si,NULL); */

  for (temp = results; temp; temp = temp->next)
    {
      res = temp->data;
      for (curstaff = si->thescore, i = 1; i < res->staff || curstaff;
	   curstaff = curstaff->next, i++)
	{

	  g_print ("Staff %d\n", i);
	  if (i == res->staff)
	    {

	      curstaffstruct = (staff *) curstaff->data;
	      for (curmeasure = (measurenode *) curstaffstruct->measures,
		   bar = 1; curmeasure; curmeasure = curmeasure->next, bar++)
		{
		  if (bar >= res->start_bar && bar <= res->end_bar)
		    {
		      beat = 1.00;
		      for (curobj = curmeasure->data; curobj;
			   curobj = curobj->next)
			{
			  g_print ("Bar %d, Beat %f\n", bar, beat);
			  mudelaitem = curobj->data;
			  if (beatcmp
			      (bar, beat, res->start_bar,
			       res->start_beat) >= 0
			      || beatcmp (bar, beat, res->end_bar,
					  res->end_beat) >= 0)
			    {
			      mudelaitem->u.chordval.is_highlighted = TRUE;
			      g_print ("Set Highlight %d, %f \n", bar, beat);
			    }
			  beat += durationtofloat
			    (mudelaitem->u.chordval.baseduration,
			     mudelaitem->u.chordval.numdots);
			}

		    }
		}
	    }
	}
    }
  counter++;
  gtk_widget_draw (si->scorearea, NULL);
}


void
perform_analysis (gpointer callback_data, GtkWidget * widget)
{
  static GString *filename = NULL;
  static GString *patternname = NULL;
  static GString *passtosystem = NULL;
  static GString *resfilename = NULL;
  pid_t pid;
  int status;
  struct scoreinfo *si = callback_data;

  if (!filename)
    {
      filename = g_string_new (locatedotdenemo ());
      g_string_append (filename, "/denemoanalysis");
      patternname = g_string_new (locatedotdenemo ());
      g_string_append (patternname, "/denemoanalysispattern");
      passtosystem = g_string_new (NULL);
      resfilename = g_string_new (locatedotdenemo ());
      g_string_append (resfilename, "/denemoanalysisresults");
    }

  g_print ("%s \n %s", filename->str, patternname->str);



#ifdef G_OS_WIN32
  pid = -1;
#else
  pid = fork ();
#endif

  if (pid == -1)
    {
      /*
       * Fork failed to create a process :
       */
      fprintf (stderr, "%s: Failed to fork()\n", strerror (errno));
      return;

    }
  else if (pid == 0)
    {
      filesave (filename->str, si, 0, 0, 0);
      filesave (patternname->str, si, 0, 0, 0);
      g_string_append (passtosystem, "simulation");
      g_string_append (passtosystem, " -m -a 1 ");
      g_string_append (passtosystem, "-s");
      g_string_append (passtosystem, filename->str);
      g_string_append (passtosystem, " -p");
      g_string_append (passtosystem, patternname->str);
      g_string_append (passtosystem, " -r");
      g_string_append (passtosystem, resfilename->str);
      status = system (passtosystem->str);

      _exit (0);


    }

}

int
beatcmp (int bar1, int beat1, int bar2, int beat2)
{

  if (bar1 < bar2 && beat1 < beat2)
    return -1;
  else if (bar1 == bar2 && beat1 == beat2)
    return 0;
  else
    return 1;
}

void
read_resultsfile (gpointer callback_data, GtkWidget * widget)
{
  gchar buffer[50];
  gint num_fields;
  GString *resfile = NULL;
  Results *res = NULL;
  Results *tempres = NULL;
  FILE *fp = NULL;
  GList *temp = NULL;
  gint i = 0;

  if (!resfile)
    {
      resfile = g_string_new (locatedotdenemo ());
      g_string_append (resfile, "/denemoanalysisresults");
    }

  if ((fp = fopen (resfile->str, "r")) == NULL)
    return;
  else
    {
      while (!feof (fp))
	{
	  res = g_malloc (sizeof (Results));	/*createnewres(); */
	  if (res)
	    {
	      fscanf (fp, "%d %d %f %d %f\n", &res->staff,
		      &res->start_bar, &res->start_beat,
		      &res->end_bar, &res->end_beat);
	      g_print ("Read record %d\n", i);

	      g_print ("%d %d %f %d %f\n", res->staff,
		       res->start_bar, res->start_beat,
		       res->end_bar, res->end_beat);

	      results = g_list_insert (results, res, i);

	      i++;

	    }

	  res = NULL;

	}
      fclose (fp);
    }

  for (temp = results; temp; temp = temp->next)
    {
      tempres = (Results *) temp->data;
      g_print ("%d %d %f %d %f\n", tempres->staff,
	       tempres->start_bar, tempres->start_beat,
	       tempres->end_bar, tempres->end_beat);
    }

}


Results *createnewres (void)
{
  Results *newres = g_malloc (sizeof (Results));

  return newres;
}