Debian Codegraph @

src/engine/wavetable.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "dataErrors.h"
21 #include "engine.h"
22 #include "wavetable.h"
23 #include "../ta-log.h"
24 #include "../fileutils.h"
25
26 void DivWavetable::putWaveData(SafeWriter* w) {
27 size_t blockStartSeek, blockEndSeek;
28
29 w->write("WAVE",4);
30 blockStartSeek=w->tell();
31 w->writeI(0);
32
33 w->writeC(0); // name
34 w->writeI(len);
35 w->writeI(min);
36 w->writeI(max);
37 for (int j=0; j<len; j++) {
38 w->writeI(data[j]);
39 }
40
41 blockEndSeek=w->tell();
42 w->seek(blockStartSeek,SEEK_SET);
43 w->writeI(blockEndSeek-blockStartSeek-4);
44 w->seek(0,SEEK_END);
45 }
46
47 DivDataErrors DivWavetable::readWaveData(SafeReader& reader, short version) {
48 char magic[4];
49 reader.read(magic,4);
50 if (memcmp(magic,"WAVE",4)!=0) {
51 logV("header is invalid: %c%c%c%c",magic[0],magic[1],magic[2],magic[3]);
52 return DIV_DATA_INVALID_HEADER;
53 }
54 reader.readI(); // reserved
55
56 reader.readString(); // ignored for now
57 len=reader.readI();
58 min=reader.readI();
59 max=reader.readI();
60
61 if (len>256) {
62 logE("invalid len: %d",len);
63 return DIV_DATA_INVALID_DATA;
64 }
65
66 if (min!=0) {
67 logW("invalid min %d",min);
68 min=0;
69 }
70
71 if (max>255) {
72 logW("invalid max %d",max);
73 max=255;
74 }
75
76 for (int i=0; i<len; i++) {
77 data[i]=reader.readI();
78 }
79
80 return DIV_DATA_SUCCESS;
81 }
82
83 bool DivWavetable::save(const char* path) {
84 SafeWriter* w=new SafeWriter();
85 w->init();
86
87 // write magic
88 w->write("-Furnace waveta-",16);
89
90 // write version
91 w->writeS(DIV_ENGINE_VERSION);
92
93 // reserved
94 w->writeS(0);
95
96 putWaveData(w);
97
98 FILE* outFile=ps_fopen(path,"wb");
99 if (outFile==NULL) {
100 logE("could not save wavetable: %s!",strerror(errno));
101 w->finish();
102 return false;
103 }
104 if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
105 logW("did not write entire wavetable!");
106 }
107 fclose(outFile);
108 w->finish();
109 return true;
110 }
111
112 bool DivWavetable::saveDMW(const char* path) {
113 SafeWriter* w=new SafeWriter();
114 w->init();
115
116 // write width
117 w->writeI(len);
118
119 // check height
120 w->writeC(max);
121 if (max==255) {
122 // write as new format (because 0xff means that)
123 w->writeC(1); // format version
124 w->writeC(max); // actual height
125
126 // waveform data
127 for (int i=0; i<len; i++) {
128 w->writeI(data[i]&0xff);
129 }
130 } else {
131 // write as old format
132 for (int i=0; i<len; i++) {
133 w->writeC(data[i]);
134 }
135 }
136
137 FILE* outFile=ps_fopen(path,"wb");
138 if (outFile==NULL) {
139 logE("could not save wavetable: %s!",strerror(errno));
140 w->finish();
141 return false;
142 }
143 if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
144 logW("did not write entire wavetable!");
145 }
146 fclose(outFile);
147 w->finish();
148 return true;
149 }
150
151 bool DivWavetable::saveRaw(const char* path) {
152 SafeWriter* w=new SafeWriter();
153 w->init();
154
155 // waveform data
156 for (int i=0; i<len; i++) {
157 w->writeC(data[i]);
158 }
159
160 FILE* outFile=ps_fopen(path,"wb");
161 if (outFile==NULL) {
162 logE("could not save wavetable: %s!",strerror(errno));
163 w->finish();
164 return false;
165 }
166 if (fwrite(w->getFinalBuf(),1,w->size(),outFile)!=w->size()) {
167 logW("did not write entire wavetable!");
168 }
169 fclose(outFile);
170 w->finish();
171 return true;
172 }