ReMAS  1.5
Real-time Musical Accompaniment System
FileFunctions.c
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2017 by "Information Retrieval and Parallel Computing" *
3  * group (University of Oviedo, Spain), "Interdisciplinary Computation *
4  * and Communication" group (Polytechnic University of Valencia, Spain) *
5  * and "Signal Processing and Telecommunication Systems Research" group *
6  * (University of Jaen, Spain) *
7  * Contact: remaspack@gmail.com *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  **************************************************************************
24 */
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 
38 #include "FileFunctions.h"
39 
48 int ReadParameters(DTWconst *Param, DTWfiles *NameFiles, const char *filename)
49 {
50  FILE *fp;
51 
52  int leidos=0, i=0;
53 
54  CHECKNULL(fp = fopen(filename,"r"));
55 
56  leidos += fscanf(fp, "%d\n", &Param->N_BASES);
57  leidos += fscanf(fp, "%d\n", &Param->N_STATES);
58 
59  #ifdef SIMPLE
60  leidos += fscanf(fp, "%f\n", &Param->NoteLen);
61  leidos += fscanf(fp, "%f\n", &Param->ALPHA);
62  #else
63  leidos += fscanf(fp, "%lf\n", &Param->NoteLen);
64  leidos += fscanf(fp, "%lf\n", &Param->ALPHA);
65  #endif
66  Param->ALPHA = -(Param->ALPHA);
67 
68  NameFiles->file_hanning =(char *)malloc(1024);
69  NameFiles->file_frame =(char *)malloc(1024);
70  NameFiles->file_partitura =(char *)malloc(1024);
71  NameFiles->file_kmax =(char *)malloc(1024);
72  NameFiles->file_kmin =(char *)malloc(1024);
73  NameFiles->fileStates_Time_e=(char *)malloc(1024);
74  NameFiles->fileStates_Time_i=(char *)malloc(1024);
75  NameFiles->fileStates_seq =(char *)malloc(1024);
76  NameFiles->fileStates_corr =(char *)malloc(1024);
77 
78  leidos += fscanf(fp, "%s\n", NameFiles->file_hanning);
79  leidos += fscanf(fp, "%s\n", NameFiles->file_frame);
80  leidos += fscanf(fp, "%s\n", NameFiles->file_partitura);
81  leidos += fscanf(fp, "%s\n", NameFiles->file_kmax);
82  leidos += fscanf(fp, "%s\n", NameFiles->file_kmin);
83  leidos += fscanf(fp, "%s\n", NameFiles->fileStates_Time_e);
84  leidos += fscanf(fp, "%s\n", NameFiles->fileStates_Time_i);
85  leidos += fscanf(fp, "%s\n", NameFiles->fileStates_seq);
86  leidos += fscanf(fp, "%s\n", NameFiles->fileStates_corr);
87 
88  leidos += fscanf(fp, "%d\n", &Param->WAVorMIC);
89  leidos += fscanf(fp, "%s\n", Param->SoundID);
90  leidos += fscanf(fp, "%d\n", &Param->Time_MIC);
91 
92  leidos += fscanf(fp, "%d\n", &Param->NCliOSC);
93  if (Param->NCliOSC > MaxOSC) return ErrInfoReaded;
94 
95  for (i=0; i<Param->NCliOSC; i++)
96  {
97  leidos += fscanf(fp, "%s\n", Param->HostIP[i]);
98  leidos += fscanf(fp, "%s\n", Param->HostPort[i]);
99  }
100  fclose(fp);
101 
102  #ifdef DUMP
103  printf("Number of Bases: %d\n", Param->N_BASES);
104  printf("Number of States: %d\n", Param->N_STATES);
105  printf("Note durarion (sec.): %f\n", Param->NoteLen);
106  printf("ALPHA: %f\n", Param->ALPHA);
107  printf("Hanning File: %s\n", NameFiles->file_hanning);
108  if (Param->WAVorMIC == 0)
109  printf("Wav File: %s. USED\n", NameFiles->file_frame);
110  else
111  printf("Wav File: %s. NOT USED\n", NameFiles->file_frame);
112  printf("Score File: %s\n", NameFiles->file_partitura);
113  printf("KMAX File: %s\n", NameFiles->file_kmax);
114  printf("KMIN File: %s\n", NameFiles->file_kmin);
115  printf("States_time_e File: %s\n", NameFiles->fileStates_Time_e);
116  printf("States_time_i File: %s\n", NameFiles->fileStates_Time_i);
117  printf("States_seq File: %s\n", NameFiles->fileStates_seq);
118  printf("States_correl File: %s\n", NameFiles->fileStates_corr);
119  if (Param->WAVorMIC != 0)
120  {
121  printf("Used sound device ID: %s\n", Param->SoundID);
122  printf("Used sound MIDI time: %d\n", Param->Time_MIC);
123  }
124  printf("%d OSC clients are used. They are:\n", Param->NCliOSC);
125  for (i=0; i<Param->NCliOSC; i++)
126  {
127  printf("Host IP: %s\n", Param->HostIP[i]);
128  printf("Host Port: %s\n", Param->HostPort[i]);
129  }
130  #endif
131 
132  if (leidos != (17 + (Param->NCliOSC*2))) return ErrInfoReaded; else return OK;
133 }
134 
135 
144 int ReadVector(MyType *vector, const int size, const char *filename)
145 {
146  FILE *fp;
147 
148  CHECKNULL(fp = fopen(filename,"rb"));
149  if (fread(vector, sizeof(MyType), size, fp) != size)
150  { fclose(fp); return ErrReadFile; }
151  else
152  { fclose(fp); return OK; }
153 
154 /* MyType valor;
155  int contLineas, leidos;
156 
157  CHECKNULL(fp = fopen(filename,"rb"));
158 
159  contLineas=0;
160 
161  leidos=fread(&valor, sizeof(MyType), 1, fp);
162  if (leidos != 1) { fclose(fp); return ErrReadFile; }
163 
164  while (!feof(fp)) {
165  if (contLineas<size) vector[contLineas]=valor;
166  contLineas++;
167 
168  leidos=fread(&valor, sizeof(MyType), 1, fp);
169  if ((leidos != 1) && (!feof(fp))) { fclose(fp); return ErrReadFile; }
170  }
171  fclose(fp);
172  if (contLineas != size) return ErrInfoReaded; else return OK;
173 */
174 }
175 
176 
185 int ReadVectorInt64(int *vector, const int size, const char *filename)
186 {
187  FILE *fp;
188  int contLineas, leidos;
189 
190  // BY RANILLA 03-03-2016 13:54. Dirty, we need better solution
191  #ifdef ARM32
192  long long int valorLong;
193  int nbytes=sizeof(long long int);
194  #else
195  long int valorLong;
196  int nbytes=sizeof(long int);
197  #endif
198 
199  CHECKNULL(fp=fopen(filename, "rb"));
200 
201  contLineas=0;
202 
203  leidos=fread(&valorLong, nbytes, 1, fp);
204  if (leidos != 1) { fclose(fp); return ErrReadFile; }
205 
206  while (!feof(fp))
207  {
208  if (contLineas < size) vector[contLineas]=(int)valorLong;
209  contLineas++;
210 
211  leidos=fread(&valorLong, nbytes, 1, fp);
212  if ((leidos != 1) && (!feof(fp))) { fclose(fp); return ErrReadFile; }
213  }
214  fclose(fp);
215  if (contLineas != size) return ErrInfoReaded; else return OK;
216 
217 }
218 
219 
228 int ReadS_fk(MyType *s_fk, const int BASES, const char *filename)
229 {
230  long i, k;
231 
232  long size = N_MIDI_PAD*BASES;
233  MyType data;
234 
235  FILE *fp;
236 
237  CHECKNULL(fp=fopen(filename, "rb"));
238 
239  i=0;
240  k=fread(&data, sizeof(MyType), 1, fp);
241  if (k != 1) { fclose(fp); return ErrReadFile; }
242 
243  while(!feof(fp))
244  {
245  if (i<size)
246  {
247  s_fk[i]=data;
248  if ((i%N_MIDI_PAD)< (N_MIDI-1))
249  i++;
250  else
251  i += (N_MIDI_PAD-N_MIDI+1);
252  }
253 
254  k=fread(&data, sizeof(MyType), 1, fp);
255  if ((k != 1) && (!feof(fp))) { fclose(fp); return ErrReadFile; }
256  }
257  fclose(fp);
258  if (i != size) return ErrInfoReaded; else return OK;
259 }
260 
261 
268 void FreeFiles(DTWfiles *NameFiles)
269 {
270  free(NameFiles->file_hanning);
271  free(NameFiles->file_frame);
272  free(NameFiles->file_partitura);
273  free(NameFiles->file_kmax);
274  free(NameFiles->file_kmin);
275  free(NameFiles->fileStates_Time_e);
276  free(NameFiles->fileStates_Time_i);
277  free(NameFiles->fileStates_seq);
278  free(NameFiles->fileStates_corr);
279 }
280 
int NCliOSC
Definition: defines.h:199
char * fileStates_Time_i
Definition: defines.h:237
Struct for store the name of input/verificaton files. Each composition needs a file with values for ...
Definition: defines.h:228
int N_BASES
Definition: defines.h:192
Header file with auxiliar functions using by ReMAS, both CPU and GPU.
int ReadVectorInt64(int *vector, const int size, const char *filename)
ReadVectorInt64 fills a int vector with the int64 info stores in a file.
char HostPort[MaxOSC][5]
Definition: defines.h:201
void FreeFiles(DTWfiles *NameFiles)
FreeFiles frees the reserved memory of a struct.
int ReadVector(MyType *vector, const int size, const char *filename)
ReadVector fills a MyType vector with the MyType info stores in a file.
MyType NoteLen
Definition: defines.h:194
char * file_kmax
Definition: defines.h:234
char SoundID[32]
Definition: defines.h:197
int Time_MIC
Definition: defines.h:198
char * fileStates_corr
Definition: defines.h:239
char HostIP[MaxOSC][16]
Definition: defines.h:200
int ReadS_fk(MyType *s_fk, const int BASES, const char *filename)
ReadS_fk fills the vector s_fk with the info stores in a file.
int N_STATES
Definition: defines.h:193
int ReadParameters(DTWconst *Param, DTWfiles *NameFiles, const char *filename)
ReadParameters reads ReMAS global parameters from file.
Definition: FileFunctions.c:48
int WAVorMIC
Definition: defines.h:196
Struct for store global information of the problem. Each composition needs a file with values for th...
Definition: defines.h:190
char * fileStates_Time_e
Definition: defines.h:236
char * file_frame
Definition: defines.h:232
char * fileStates_seq
Definition: defines.h:238
char * file_kmin
Definition: defines.h:235
char * file_partitura
Definition: defines.h:233
char * file_hanning
Definition: defines.h:231
MyType ALPHA
Definition: defines.h:195