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