1 /*
2 This file is part of Deadbeef Player source code
3 http://deadbeef.sourceforge.net
4
5 dsp preset manager
6
7 Copyright (C) 2009-2013 Alexey Yakovenko
8
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any damages
11 arising from the use of this software.
12
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute it
15 freely, subject to the following restrictions:
16
17 1. The origin of this software must not be misrepresented; you must not
18 claim that you wrote the original software. If you use this software
19 in a product, an acknowledgment in the product documentation would be
20 appreciated but is not required.
21 2. Altered source versions must be plainly marked as such, and must not be
22 misrepresented as being the original software.
23 3. This notice may not be removed or altered from any source distribution.
24
25 Alexey Yakovenko waker@users.sourceforge.net
26 */
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "deadbeef.h"
32 #include "plugins.h"
33
34 void
dsp_preset_free(ddb_dsp_context_t * head)35 dsp_preset_free (ddb_dsp_context_t *head) {
36 while (head) {
37 ddb_dsp_context_t *next = head->next;
38 head->plugin->close (head);
39 head = next;
40 }
41 }
42
43 int
dsp_preset_load(const char * fname,ddb_dsp_context_t ** head)44 dsp_preset_load (const char *fname, ddb_dsp_context_t **head) {
45 if (!head) {
46 return -1;
47 }
48 int err = 1;
49 FILE *fp = fopen (fname, "rt");
50 if (!fp) {
51 return -1;
52 }
53
54 ddb_dsp_context_t *tail = NULL;
55
56 char temp[100];
57 for (;;) {
58 // plugin {
59 int err = fscanf (fp, "%99s {\n", temp);
60 if (err == EOF) {
61 break;
62 }
63 else if (1 != err) {
64 fprintf (stderr, "error plugin name\n");
65 goto error;
66 }
67
68 DB_dsp_t *plug = (DB_dsp_t *)plug_get_for_id (temp);
69 if (!plug) {
70 fprintf (stderr, "ddb_dsp_preset_load: plugin %s not found. preset will not be loaded\n", temp);
71 goto error;
72 }
73 ddb_dsp_context_t *ctx = plug->open ();
74 if (!ctx) {
75 fprintf (stderr, "ddb_dsp_preset_load: failed to open ctxance of plugin %s\n", temp);
76 goto error;
77 }
78
79 if (tail) {
80 tail->next = ctx;
81 tail = ctx;
82 }
83 else {
84 tail = *head = ctx;
85 }
86
87 int n = 0;
88 for (;;) {
89 char value[1000];
90 if (!fgets (temp, sizeof (temp), fp)) {
91 fprintf (stderr, "unexpected eof while reading plugin params\n");
92 goto error;
93 }
94 if (!strcmp (temp, "}\n")) {
95 break;
96 }
97 else if (1 != sscanf (temp, "\t%1000[^\n]\n", value)) {
98 fprintf (stderr, "error loading param %d\n", n);
99 goto error;
100 }
101 if (plug->num_params) {
102 plug->set_param (ctx, n, value);
103 }
104 n++;
105 }
106 }
107
108 err = 0;
109 error:
110 if (err) {
111 fprintf (stderr, "error loading %s\n", fname);
112 }
113 if (fp) {
114 fclose (fp);
115 }
116 if (err && *head) {
117 dsp_preset_free (*head);
118 *head = NULL;
119 }
120 return err ? -1 : 0;
121 }
122
123 int
dsp_preset_save(const char * path,ddb_dsp_context_t * head)124 dsp_preset_save (const char *path, ddb_dsp_context_t *head) {
125 FILE *fp = fopen (path, "w+t");
126 if (!fp) {
127 return -1;
128 }
129
130 ddb_dsp_context_t *ctx = head;
131 while (ctx) {
132 fprintf (fp, "%s {\n", ctx->plugin->plugin.id);
133 if (ctx->plugin->num_params) {
134 int n = ctx->plugin->num_params ();
135 int i;
136 for (i = 0; i < n; i++) {
137 char v[1000];
138 ctx->plugin->get_param (ctx, i, v, sizeof (v));
139 fprintf (fp, "\t%s\n", v);
140 }
141 }
142 fprintf (fp, "}\n");
143 ctx = ctx->next;
144 }
145
146 fclose (fp);
147 return 0;
148 }
149