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 "config.h"
24 #include "debug.h"
25 #include "i18n.h"
26 #include "interface.h"
27 #include "misc.h"
28 
29 static EqualizerPreset * equalizer_preset_new (const char * name)
30 {
31  EqualizerPreset *preset = g_new0(EqualizerPreset, 1);
32  preset->name = g_strdup(name);
33  return preset;
34 }
35 
36 Index * equalizer_read_presets (const char * basename)
37 {
38  char *filename, *name;
39  GKeyFile *rcfile;
40  GError *error = NULL;
41  int i, p = 0;
42  EqualizerPreset *preset;
43 
44  filename = g_build_filename (get_path (AUD_PATH_USER_DIR), basename, NULL);
45 
46  rcfile = g_key_file_new();
47  if (!g_key_file_load_from_file(rcfile, filename, G_KEY_FILE_NONE, &error))
48  {
49  g_free(filename);
50  filename = g_build_filename (get_path (AUD_PATH_DATA_DIR), basename,
51  NULL);
52 
53  error = NULL;
54  if (!g_key_file_load_from_file(rcfile, filename, G_KEY_FILE_NONE, &error))
55  {
56  g_free(filename);
57  return NULL;
58  }
59  }
60 
61  g_free(filename);
62 
63  Index * list = index_new ();
64 
65  for (;;)
66  {
67  char section[32];
68 
69  error = NULL;
70  g_snprintf(section, sizeof(section), "Preset%d", p++);
71 
72  if ((name = g_key_file_get_string(rcfile, "Presets", section, &error)) != NULL)
73  {
74  error = NULL;
75 
76  preset = g_new0(EqualizerPreset, 1);
77  preset->name = name;
78  preset->preamp = g_key_file_get_double(rcfile, name, "Preamp", &error);
79 
80  for (i = 0; i < AUD_EQUALIZER_NBANDS; i++)
81  {
82  char band[16];
83  g_snprintf(band, sizeof(band), "Band%d", i);
84 
85  error = NULL;
86 
87  preset->bands[i] = g_key_file_get_double(rcfile, name, band, &error);
88  }
89 
90  index_append (list, preset);
91  }
92  else
93  break;
94  }
95 
96  g_key_file_free(rcfile);
97 
98  return list;
99 }
100 
101 bool_t equalizer_write_preset_file (Index * list, const char * basename)
102 {
103  char *filename;
104  int i;
105  GKeyFile *rcfile;
106  char *data;
107  gsize len;
108  GError *error = NULL;
109 
110  rcfile = g_key_file_new();
111 
112  for (int p = 0; p < index_count (list); p ++)
113  {
114  EqualizerPreset * preset = index_get (list, p);
115 
116  char * tmp = g_strdup_printf ("Preset%d", p);
117  g_key_file_set_string(rcfile, "Presets", tmp, preset->name);
118  g_free(tmp);
119 
120  g_key_file_set_double(rcfile, preset->name, "Preamp", preset->preamp);
121 
122  for (i = 0; i < 10; i++)
123  {
124  tmp = g_strdup_printf("Band%d", i);
125  g_key_file_set_double(rcfile, preset->name, tmp,
126  preset->bands[i]);
127  g_free(tmp);
128  }
129  }
130 
131  filename = g_build_filename (get_path (AUD_PATH_USER_DIR), basename, NULL);
132 
133  data = g_key_file_to_data(rcfile, &len, &error);
134  bool_t success = g_file_set_contents (filename, data, len, & error);
135  g_free(data);
136 
137  g_key_file_free(rcfile);
138  g_free(filename);
139  return success;
140 }
141 
142 Index * import_winamp_eqf (VFSFile * file)
143 {
144  char header[31];
145  char bands[11];
146  int i = 0;
147  EqualizerPreset *preset = NULL;
148  char *markup;
149  char preset_name[0xb4];
150 
151  if (vfs_fread (header, 1, sizeof header, file) != sizeof header || strncmp
152  (header, "Winamp EQ library file v1.1", 27))
153  goto error;
154 
155  AUDDBG("The EQF header is OK\n");
156 
157  if (vfs_fseek(file, 0x1f, SEEK_SET) == -1) goto error;
158 
159  Index * list = index_new ();
160 
161  while (vfs_fread(preset_name, 1, 0xb4, file) == 0xb4) {
162  AUDDBG("The preset name is '%s'\n", preset_name);
163  if (vfs_fseek (file, 0x4d, SEEK_CUR)) /* unknown crap --asphyx */
164  break;
165  if (vfs_fread(bands, 1, 11, file) != 11) break;
166 
167  preset = equalizer_preset_new(preset_name);
168  /*this was divided by 63, but shouldn't it be 64? --majeru*/
169  preset->preamp = EQUALIZER_MAX_GAIN - ((bands[10] * EQUALIZER_MAX_GAIN * 2) / 64.0);
170 
171  for (i = 0; i < 10; i++)
172  preset->bands[i] = EQUALIZER_MAX_GAIN - ((bands[i] * EQUALIZER_MAX_GAIN * 2) / 64.0);
173 
174  index_append (list, preset);
175  }
176 
177  return list;
178 
179 error:
180  markup = g_strdup_printf (_("Error importing Winamp EQF file '%s'"),
181  vfs_get_filename (file));
182  interface_show_error(markup);
183 
184  g_free(markup);
185  return NULL;
186 }
187 
189 {
190  GKeyFile *rcfile;
191  int i;
192  char *data;
193  gsize len;
194  GError *error = NULL;
195 
196  rcfile = g_key_file_new();
197  g_key_file_set_double(rcfile, "Equalizer preset", "Preamp", preset->preamp);
198 
199  for (i = 0; i < 10; i++) {
200  char tmp[7];
201  g_snprintf(tmp, sizeof(tmp), "Band%d", i);
202  g_key_file_set_double(rcfile, "Equalizer preset", tmp,
203  preset->bands[i]);
204  }
205 
206  data = g_key_file_to_data(rcfile, &len, &error);
207 
208  bool_t success = FALSE;
209 
210  VFSFile * file = vfs_fopen (filename, "w");
211  if (file == NULL)
212  goto DONE;
213  if (vfs_fwrite (data, 1, strlen (data), file) == strlen (data))
214  success = TRUE;
215  vfs_fclose (file);
216 
217 DONE:
218  g_free(data);
219  g_key_file_free(rcfile);
220  return success;
221 }
222 
224 {
225  int i;
226  EqualizerPreset *preset;
227  GKeyFile *rcfile;
228  GError *error = NULL;
229 
230  preset = g_new0(EqualizerPreset, 1);
231  preset->name = g_strdup("");
232 
233  rcfile = g_key_file_new();
234  if (!g_key_file_load_from_file(rcfile, filename, G_KEY_FILE_NONE, &error))
235  {
236  g_key_file_free(rcfile);
237  g_free(preset->name);
238  g_free(preset);
239  return NULL;
240  }
241 
242  preset->preamp = g_key_file_get_double(rcfile, "Equalizer preset", "Preamp", &error);
243  for (i = 0; i < 10; i++)
244  {
245  char tmp[7];
246  g_snprintf(tmp, sizeof(tmp), "Band%d", i);
247 
248  preset->bands[i] = g_key_file_get_double(rcfile, "Equalizer preset", tmp, &error);
249  }
250 
251  g_key_file_free(rcfile);
252  return preset;
253 }
254 
257 {
258  if (filename) {
259  EqualizerPreset *preset = equalizer_read_aud_preset(filename);
260  return preset;
261  }
262  return NULL;
263 }