1 #include "edje_multisense_convert.h"
6 # include <vorbis/vorbisenc.h>
10 # include <FLAC/metadata.h>
11 # include <FLAC/stream_encoder.h>
15 _edje_multisense_encode(const char *filename, Edje_Sound_Sample *sample, double quality __UNUSED__)
19 Edje_Sound_Encode *enc_info;
21 enc_info = calloc(1, sizeof(Edje_Sound_Encode));
24 ERR("while allocating memory to load file ");
27 memset (&sfinfo, 0, sizeof (SF_INFO));
29 enc_info->encoded = EINA_FALSE;
30 enc_info->comp_type = "RAW PCM";
32 // Open wav file using sndfile
33 sfile = sf_open (filename, SFM_READ, &sfinfo);
36 ERR("Unable to open audio file: %s", filename);
40 if (!sf_format_check(&sfinfo))
42 ERR("Unknown file, not a valid audio file");
46 if (sample->compression == EDJE_SOUND_SOURCE_TYPE_INLINE_COMP)
50 //encode provided wav file to flac
51 enc_info->file = _edje_multisense_encode_to_flac((char *)filename, sfinfo);
54 enc_info->comp_type = "FLAC";
55 enc_info->encoded = EINA_TRUE;
58 WRN("WARNING: Unable to encode sound %s to FLAC compression",
62 else if (sample->compression == EDJE_SOUND_SOURCE_TYPE_INLINE_LOSSY)
66 //encode provided wav file to ogg-vorbis
67 enc_info->file = _edje_multisense_encode_to_ogg_vorbis((char *)filename,
71 enc_info->comp_type = "OGG-VORBIS";
72 enc_info->encoded = EINA_TRUE;
75 WRN("WARNING: Unable to encode sound %s to Ogg-Vorbis",
80 eina_stringshare_replace(&enc_info->file, filename);
86 _edje_multisense_encode_to_flac(char *snd_path, SF_INFO sfinfo)
88 unsigned int total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
90 FLAC__StreamEncoder *encoder = 0;
91 FLAC__StreamEncoderInitStatus init_status;
92 FLAC__StreamMetadata *metadata[2];
93 FLAC__StreamMetadata_VorbisComment_Entry entry;
98 sfile = sf_open(snd_path, SFM_READ, &sfinfo);
99 if (!sfile) return NULL;
100 if (!sf_format_check(&sfinfo))
105 size = sf_seek(sfile, 0, SEEK_END);
106 sf_seek(sfile, 0, SEEK_SET);
107 tmp = malloc(strlen(snd_path) + 1 + 5);
113 strcpy(tmp, snd_path);
115 strcat(snd_path, ".flac");
117 total_samples = size;
119 /* allocate the encoder */
120 if ((encoder = FLAC__stream_encoder_new()) == NULL)
122 ERR("ERROR: Creating FLAC encoder\n");
128 /* Verify it's own encoded output. This will slow the encoding process. */
129 ok &= FLAC__stream_encoder_set_verify(encoder, 1);
131 //Levels range from 0 (fastest, least compression) to 8 (slowest, most compression).
132 //A value larger than 8 will be treated as 8.
133 //5 is used for good compression and moderate compression/decompression speed.
134 ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
135 ok &= FLAC__stream_encoder_set_channels(encoder, sfinfo.channels);
136 ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, 16);
137 ok &= FLAC__stream_encoder_set_sample_rate(encoder, sfinfo.samplerate);
138 ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples);
140 /* now add some metadata; we'll add some tags and a padding block */
143 if ((metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL
144 || (metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL
145 || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "Encoder", "flac")
146 || !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, 0))
148 ERR("ERROR: out of memory error or tag error\n");
151 metadata[1]->length = 16; /* set the padding length */
152 ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
155 /* initialize encoder */
158 init_status = FLAC__stream_encoder_init_file(encoder, snd_path, NULL,
159 (void *)(long)(total_samples));
160 if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
162 ERR("ERROR: unable to initialize FLAC encoder: %s\n",
163 FLAC__StreamEncoderInitStatusString[init_status]);
168 /* read blocks of samples from WAVE file and feed to encoder */
171 FLAC__int32 readbuffer[READBUF * 2];
175 count = sf_readf_int(sfile, readbuffer, READBUF);
176 if (count <= 0) break;
177 for (i = 0; i < (count * sfinfo.channels); i++)
178 readbuffer[i] = readbuffer[i] >> 16;
179 ok = FLAC__stream_encoder_process_interleaved(encoder, readbuffer,
183 FLAC__stream_encoder_finish(encoder);
184 /* now that encoding is finished, the metadata can be freed */
185 FLAC__metadata_object_delete(metadata[0]);
186 FLAC__metadata_object_delete(metadata[1]);
188 FLAC__stream_encoder_delete(encoder);
196 _edje_multisense_encode_to_ogg_vorbis(char *snd_path, double quality, SF_INFO sfinfo)
198 ogg_stream_state os; /* take physical pages, weld into a logical stream of packets */
199 ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
200 ogg_packet op; /* one raw packet of data for decode */
201 vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */
202 vorbis_comment vc; /* struct that stores all the user comments */
203 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
204 vorbis_block vb; /* local working space for packet->PCM decode */
210 sfile = sf_open(snd_path, SFM_READ, &sfinfo);
211 if (!sfile) return NULL;
212 if (!sf_format_check(&sfinfo))
217 tmp = malloc(strlen(snd_path) + 1 + 4);
223 strcpy(tmp, snd_path);
225 strcat(snd_path, ".ogg");
226 fout = fopen(snd_path, "wb");
234 /********** Encode setup ************/
235 vorbis_info_init(&vi);
236 ret = vorbis_encode_init(&vi, sfinfo.channels, sfinfo.samplerate,
237 -1, (long)(quality * 1000), -1);
238 if (ret == OV_EFAULT) printf("OV_EFAULT\n");
239 if (ret == OV_EINVAL) printf("OV_EINVAL\n");
240 if (ret == OV_EIMPL) printf("OV_EIMPL\n");
251 vorbis_comment_init(&vc);
252 vorbis_comment_add_tag(&vc, "", "");
254 /* set up the analysis state and auxiliary encoding storage */
255 vorbis_analysis_init(&vd, &vi);
256 vorbis_block_init(&vd, &vb);
259 ogg_stream_init(&os, rand());
262 ogg_packet header_comm;
263 ogg_packet header_code;
265 vorbis_analysis_headerout(&vd, &vc, &header, &header_comm, &header_code);
266 ogg_stream_packetin(&os, &header); /* automatically placed in its own page */
267 ogg_stream_packetin(&os, &header_comm);
268 ogg_stream_packetin(&os, &header_code);
272 int result = ogg_stream_flush(&os, &og);
274 fwrite(og.header, 1, og.header_len, fout);
275 fwrite(og.body, 1, og.body_len, fout);
281 float readbuffer[READBUF * 2];
284 count = sf_readf_float(sfile, readbuffer, READBUF);
287 vorbis_analysis_wrote(&vd, 0);
290 float **buffer = vorbis_analysis_buffer(&vd, count);
292 /* uninterleave samples */
293 for (i = 0; i < count; i++)
295 for (ch = 0; ch < sfinfo.channels; ch++)
296 buffer[ch][i]= readbuffer[(i * sfinfo.channels) + ch];
298 vorbis_analysis_wrote(&vd, i);
300 while (vorbis_analysis_blockout(&vd, &vb) == 1)
302 vorbis_analysis(&vb, NULL);
303 vorbis_bitrate_addblock(&vb);
305 while (vorbis_bitrate_flushpacket(&vd, &op))
307 ogg_stream_packetin(&os, &op);
310 int result = ogg_stream_pageout(&os, &og);
312 fwrite(og.header, 1, og.header_len, fout);
313 fwrite(og.body, 1, og.body_len, fout);
314 if (ogg_page_eos(&og)) eos = 1;
319 ogg_stream_clear(&os);
320 vorbis_block_clear(&vb);
321 vorbis_dsp_clear(&vd);
322 vorbis_comment_clear(&vc);
323 vorbis_info_clear(&vi);