Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
equalizer_preset.c
Go to the documentation of this file.
1 /*
2  * equalizer_preset.c
3  * Copyright 2003-2011 Eugene Zagidullin, William Pitcock, and John Lindgren
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <string.h>
22 
23 #include "debug.h"
24 #include "i18n.h"
25 #include "interface.h"
26 #include "misc.h"
27 
28 static EqualizerPreset * equalizer_preset_new (const char * name)
29 {
30  EqualizerPreset *preset = g_new0(EqualizerPreset, 1);
31  preset->name = g_strdup(name);
32  return preset;
33 }
34 
35 Index * equalizer_read_presets (const char * basename)
36 {
37  char *filename, *name;
38  GKeyFile *rcfile;
39  GError *error = NULL;
40  int i, p = 0;
41  EqualizerPreset *preset;
42 
43  filename = g_build_filename (get_path (AUD_PATH_USER_DIR), basename, NULL);
44 
45  rcfile = g_key_file_new();
46  if (!g_key_file_load_from_file(rcfile, filename, G_KEY_FILE_NONE, &error))
47  {
48  g_free(filename);
49  filename = g_build_filename (get_path (AUD_PATH_DATA_DIR), basename,
50  NULL);
51 
52  error = NULL;
53  if (!g_key_file_load_from_file(rcfile, filename, G_KEY_FILE_NONE, &error))
54  {
55  g_free(filename);
56  return NULL;
57  }
58  }
59 
60  g_free(filename);
61 
62  Index * list = index_new ();
63 
64  for (;;)
65  {
66  char section[32];
67 
68  error = NULL;
69  g_snprintf(section, sizeof(section), "Preset%d", p++);
70 
71  if ((name = g_key_file_get_string(rcfile, "Presets", section, &error)) != NULL)
72  {
73  error = NULL;
74 
75  preset = g_new0(EqualizerPreset, 1);
76  preset->name = name;
77  preset->preamp = g_key_file_get_double(rcfile, name, "Preamp", &error);
78 
79  for (i = 0; i < AUD_EQUALIZER_NBANDS; i++)
80  {
81  char band[16];
82  g_snprintf(band, sizeof(band), "Band%d", i);
83 
84  error = NULL;
85 
86  preset->bands[i] = g_key_file_get_double(rcfile, name, band, &error);
87  }
88 
89  index_append (list, preset);
90  }
91  else
92  break;
93  }
94 
95  g_key_file_free(rcfile);
96 
97  return list;
98 }
99 
100 bool_t equalizer_write_preset_file (Index * list, const char * basename)
101 {
102  char *filename;
103  int i;
104  GKeyFile *rcfile;
105  char *data;
106  gsize len;
107  GError *error = NULL;
108 
109  rcfile = g_key_file_new();
110 
111  for (int p = 0; p < index_count (list); p ++)
112  {
113  EqualizerPreset * preset = index_get (list, p);
114 
115  char * tmp = g_strdup_printf ("Preset%d", p);
116  g_key_file_set_string(rcfile, "Presets", tmp, preset->name);
117  g_free(tmp);
118 
119  g_key_file_set_double(rcfile, preset->name, "Preamp", preset->preamp);
120 
121  for (i = 0; i < 10; i++)
122  {
123  tmp = g_strdup_printf("Band%d", i);
124  g_key_file_set_double(rcfile, preset->name, tmp,
125  preset->bands[i]);
126  g_free(tmp);
127  }
128  }
129 
130  filename = g_build_filename (get_path (AUD_PATH_USER_DIR), basename, NULL);
131 
132  data = g_key_file_to_data(rcfile, &len, &error);
133  bool_t success = g_file_set_contents (filename, data, len, & error);
134  g_free(data);
135 
136  g_key_file_free(rcfile);
137  g_free(filename);
138  return success;
139 }
140 
141 Index * import_winamp_eqf (VFSFile * file)
142 {
143  char header[31];
144  char bands[11];
145  int i = 0;
146  EqualizerPreset *preset = NULL;
147  char *markup;
148  char preset_name[0xb4];
149 
150  if (vfs_fread (header, 1, sizeof header, file) != sizeof header || strncmp
151  (header, "Winamp EQ library file v1.1", 27))
152  goto error;
153 
154  AUDDBG("The EQF header is OK\n");
155 
156  if (vfs_fseek(file, 0x1f, SEEK_SET) == -1) goto error;
157 
158  Index * list = index_new ();
159 
160  while (vfs_fread(preset_name, 1, 0xb4, file) == 0xb4) {
161  AUDDBG("The preset name is '%s'\n", preset_name);
162  if (vfs_fseek (file, 0x4d, SEEK_CUR)) /* unknown crap --asphyx */
163  break;
164  if (vfs_fread(bands, 1, 11, file) != 11) break;
165 
166  preset = equalizer_preset_new(preset_name);
167  /*this was divided by 63, but shouldn't it be 64? --majeru*/
168  preset->preamp = EQUALIZER_MAX_GAIN - ((bands[10] * EQUALIZER_MAX_GAIN * 2) / 64.0);
169 
170  for (i = 0; i < 10; i++)
171  preset->bands[i] = EQUALIZER_MAX_GAIN - ((bands[i] * EQUALIZER_MAX_GAIN * 2) / 64.0);
172 
173  index_append (list, preset);
174  }
175 
176  return list;
177 
178 error:
179  markup = g_strdup_printf (_("Error importing Winamp EQF file '%s'"),
180  vfs_get_filename (file));
181  interface_show_error(markup);
182 
183  g_free(markup);
184  return NULL;
185 }
186 
188 {
189  GKeyFile *rcfile;
190  int i;
191  char *data;
192  gsize len;
193  GError *error = NULL;
194 
195  rcfile = g_key_file_new();
196  g_key_file_set_double(rcfile, "Equalizer preset", "Preamp", preset->preamp);
197 
198  for (i = 0; i < 10; i++) {
199  char tmp[7];
200  g_snprintf(tmp, sizeof(tmp), "Band%d", i);
201  g_key_file_set_double(rcfile, "Equalizer preset", tmp,
202  preset->bands[i]);
203  }
204 
205  data = g_key_file_to_data(rcfile, &len, &error);
206 
207  bool_t success = FALSE;
208 
209  VFSFile * file = vfs_fopen (filename, "w");
210  if (file == NULL)
211  goto DONE;
212  if (vfs_fwrite (data, 1, strlen (data), file) == strlen (data))
213  success = TRUE;
214  vfs_fclose (file);
215 
216 DONE:
217  g_free(data);
218  g_key_file_free(rcfile);
219  return success;
220 }
221 
223 {
224  int i;
225  EqualizerPreset *preset;
226  GKeyFile *rcfile;
227  GError *error = NULL;
228 
229  preset = g_new0(EqualizerPreset, 1);
230  preset->name = g_strdup("");
231 
232  rcfile = g_key_file_new();
233  if (!g_key_file_load_from_file(rcfile, filename, G_KEY_FILE_NONE, &error))
234  {
235  g_key_file_free(rcfile);
236  g_free(preset->name);
237  g_free(preset);
238  return NULL;
239  }
240 
241  preset->preamp = g_key_file_get_double(rcfile, "Equalizer preset", "Preamp", &error);
242  for (i = 0; i < 10; i++)
243  {
244  char tmp[7];
245  g_snprintf(tmp, sizeof(tmp), "Band%d", i);
246 
247  preset->bands[i] = g_key_file_get_double(rcfile, "Equalizer preset", tmp, &error);
248  }
249 
250  g_key_file_free(rcfile);
251  return preset;
252 }
253 
256 {
257  if (filename) {
258  EqualizerPreset *preset = equalizer_read_aud_preset(filename);
259  return preset;
260  }
261  return NULL;
262 }