-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExporter.h
More file actions
231 lines (172 loc) · 7.39 KB
/
Exporter.h
File metadata and controls
231 lines (172 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
#include "AppState.h"
#include <stdio.h>
#include <lame.h>
#include <juce_audio_basics/juce_audio_basics.h>
class Exporter
{
public:
enum class ExportFormat {
WAV,
MP3,
FLAC,
OGG
};
Exporter() {
// Initialize encoders if needed
}
// Main export function that delegates to specific format exporters
bool exportBuffer(const float* leftChannel, const float* rightChannel, int bufferSize,
double sampleRate, const juce::String& applicationName, ExportFormat format, juce::String& recordingsLocation) {
// Create a unique filename based on current time
auto now = juce::Time::getCurrentTime();
auto formattedDate = now.formatted("%Y-%m-%d_%H-%M-%S");
juce::String formatExtension = getFormatExtension(format);
juce::String filename = formattedDate;
if (applicationName.isNotEmpty())
filename += "_" + applicationName;
filename += formatExtension;
juce::File directory;
if (recordingsLocation.isEmpty()) {
directory = juce::File::getSpecialLocation(juce::File::userDesktopDirectory);
} else {
directory = juce::File(recordingsLocation);
// Make sure the directory exists and is writable
if (!directory.exists() || !directory.isDirectory() || !directory.hasWriteAccess()) {
std::cout << "Warning: Recordings location is not accessible, falling back to desktop" << std::endl;
directory = juce::File::getSpecialLocation(juce::File::userDesktopDirectory);
}
}
auto file = directory.getChildFile(filename);
std::cout << "Exporting to: " << file.getFullPathName() << std::endl;
// Create AudioBuffer from the input channels
juce::AudioBuffer<float> buffer;
buffer.setSize(2, bufferSize, false, true, true);
for (int i = 0; i < bufferSize; i++) {
buffer.setSample(0, i, leftChannel[i]);
buffer.setSample(1, i, rightChannel[i]);
}
bool success = false;
switch (format) {
case ExportFormat::WAV:
success = exportAsWav(buffer, file, sampleRate);
break;
case ExportFormat::FLAC:
success = exportAsFlac(buffer, file, sampleRate);
break;
case ExportFormat::MP3:
success = exportAsMp3(buffer, file, sampleRate);
break;
case ExportFormat::OGG:
success = exportAsOgg(buffer, file, sampleRate);
break;
}
if (success) {
// Initiate drag-and-drop of the exported file
return juce::DragAndDropContainer::performExternalDragDropOfFiles({file.getFullPathName()}, false, nullptr);
}
// Clean up failed export
if (file.existsAsFile()) {
file.deleteFile();
}
return false;
}
private:
juce::String getFormatExtension(ExportFormat format) {
switch (format) {
case ExportFormat::WAV: return ".wav";
case ExportFormat::FLAC: return ".flac";
case ExportFormat::MP3: return ".mp3";
case ExportFormat::OGG: return ".ogg";
default: return ".wav";
}
}
bool exportAsWav(juce::AudioBuffer<float>& buffer, juce::File& file, double sampleRate) {
juce::WavAudioFormat wavFormat;
juce::FileOutputStream* fileStream = new juce::FileOutputStream(file);
if (fileStream->failedToOpen()) {
delete fileStream;
return false;
}
fileStream->setPosition(0);
fileStream->truncate();
std::unique_ptr<juce::AudioFormatWriter> writer(wavFormat.createWriterFor(
fileStream, sampleRate, buffer.getNumChannels(), 32, {}, 0));
if (writer == nullptr) {
delete fileStream;
return false;
}
writer->writeFromAudioSampleBuffer(buffer, 0, buffer.getNumSamples());
fileStream->flush();
return true;
}
bool exportAsFlac(juce::AudioBuffer<float>& buffer, juce::File& file, double sampleRate) {
juce::FlacAudioFormat flacFormat;
juce::FileOutputStream* fileStream = new juce::FileOutputStream(file);
if (fileStream->failedToOpen()) {
delete fileStream;
return false;
}
fileStream->setPosition(0);
fileStream->truncate();
std::unique_ptr<juce::AudioFormatWriter> writer(flacFormat.createWriterFor(
fileStream, sampleRate, buffer.getNumChannels(), 16, {}, 0));
if (writer == nullptr) {
delete fileStream;
return false;
}
writer->writeFromAudioSampleBuffer(buffer, 0, buffer.getNumSamples());
fileStream->flush();
return true;
}
bool exportAsMp3(juce::AudioBuffer<float>& buffer, juce::File& file, double sampleRate) {
// parameters
int quality = 2; /* 2=high 5 = medium 7=low */
int mode = 1; // mode = 0,1,2,3 = stereo, jstereo, dual channel (not supported), mono
lame_global_flags *lameFlags = lame_init();
lame_set_num_channels(lameFlags, buffer.getNumChannels());
lame_set_in_samplerate(lameFlags, sampleRate);
lame_set_brate(lameFlags, 320);
lame_set_mode(lameFlags, (MPEG_mode)mode);
lame_set_quality(lameFlags, quality);
auto ret_code = lame_init_params(lameFlags);
if (ret_code < 0) {
printf("Error (ret_code): %d\n", ret_code);
return false;
}
int mp3buf_size = 1.1 * buffer.getNumSamples() + 7200;
unsigned char* mp3buf = new unsigned char[mp3buf_size];
auto err = lame_encode_buffer_ieee_float(lameFlags, buffer.getReadPointer(0), buffer.getReadPointer(1), buffer.getNumSamples(), mp3buf, mp3buf_size);
if (err < 0) {
printf("Error (err): %d\n", err);
return false;
}
lame_encode_flush(lameFlags, mp3buf, mp3buf_size);
FILE* mp3 = fopen(file.getFullPathName().toRawUTF8(), "wb");
lame_mp3_tags_fid(lameFlags, mp3);
fwrite(mp3buf, 1, mp3buf_size, mp3);
lame_close(lameFlags);
fclose(mp3);
delete[] mp3buf;
return true;
}
bool exportAsOgg(juce::AudioBuffer<float>& buffer, juce::File& file, double sampleRate) {
juce::OggVorbisAudioFormat oggFormat;
juce::FileOutputStream* fileStream = new juce::FileOutputStream(file);
if (fileStream->failedToOpen()) {
delete fileStream;
return false;
}
fileStream->setPosition(0);
fileStream->truncate();
std::unique_ptr<juce::AudioFormatWriter> writer(oggFormat.createWriterFor(
fileStream, sampleRate, buffer.getNumChannels(), 0, {}, 0));
if (writer == nullptr) {
delete fileStream;
return false;
}
writer->writeFromAudioSampleBuffer(buffer, 0, buffer.getNumSamples());
fileStream->flush();
return true;
}
};