void Parser::setBuffer(string arqName, vector<vector<vector<uint8_t>>> value, int nbSamplesIn, int sampleFormatIn, int sampleRateIn, uint64_t channelLayoutIn, int nbChannelsIn) { int error; this->fileName = arqName; this->bufFrames = value; this->nbSamplesIn = nbSamplesIn; this->sampleFormatIn = sampleFormatIn; this->sampleRateIn = sampleRateIn; this->channelLayoutIn = channelLayoutIn; this->nbChannelsIn = nbChannelsIn; if (swr_ctx) swr_free(&swr_ctx); InitResampler(); if (audioFormat == AUDIOFORMAT::arq) { AVIOContext **io_ctx = &fmt_ctx_out->pb; if (*io_ctx) avio_close(*io_ctx); error = avio_open(io_ctx, fileName.c_str(), AVIO_FLAG_WRITE); if (error < 0) { char error_buffer[255]; av_strerror(error, error_buffer, sizeof(error_buffer)); objLog->mr_printf(MR_LOG_ERROR, idRadio, "Error: %s - %s\n", fileName.c_str(), error_buffer); throw ConvertException() << errno_code(MIR_ERR_OPEN_OUTPUT_FILE); } av_dump_format(fmt_ctx_out, 0, fileName.c_str(), 1); error = avformat_write_header(fmt_ctx_out, NULL); if (error < 0) { char error_buffer[255]; av_strerror(error, error_buffer, sizeof(error_buffer)); objLog->mr_printf(MR_LOG_ERROR, idRadio, "Error: %s - %s\n", fileName.c_str(), error_buffer); throw StreamException() << errno_code(MIR_ERR_OPEN_STREAM_3); } } }
void Parser::Config() { try { nbBuffers = av_sample_fmt_is_planar((AVSampleFormat)sampleFormatIn) ? this->nbChannelsIn : 1; // cria o contexto de saída CreateContext(); // seta os dados do encoder setStream(); av_dump_format(fmt_ctx_out, 0, fileName.c_str(), 1); createFrames(); // inicia o resample InitResampler(); } catch(...) { throw; } }
int main(int argc, char **argv) { FILE *infile, *outfile; short *inbuf, *outbuf; int inbufsize, outbufsize; int argnext, incount, outcount, starttime; int nread, ninput, noutput, nwanted, nsaved; float calctime, audiotime; void *inst; /* resampler instance */ /* command-line switches */ argnext = ParseArgs(argc, argv); if (argc - argnext != 2) Usage(argv[0]); /* open files */ if ((infile = fopen(argv[argnext++], "rb")) == NULL) ERROR("Cannot open infile"); if ((outfile = fopen(argv[argnext++], "wb")) == NULL) ERROR("Cannot open outfile"); /* create resampler */ inst = InitResampler(inrate, outrate, chans, quality); if (!inst) ERROR("InitResampler failed"); /* determine buffer sizes */ if (!outmode) { /* constant-input */ inbufsize = nchunk; outbufsize = GetMaxOutput(nchunk, inst); } else { /* constant-output */ inbufsize = GetMinInput(nchunk, inst); outbufsize = nchunk + GetMaxOutput(chans, inst); } /* allocate buffers */ inbuf = (short *) malloc(inbufsize * sizeof(short)); outbuf = (short *) malloc(outbufsize * sizeof(short)); if (!inbuf || !outbuf) ERROR("malloc failed"); printf("\nConverting %d %s to %d %s (quality=%d %s=%d)\n", inrate, chans==2 ? "STEREO" : "MONO", outrate, chans==2 ? "STEREO" : "MONO", quality, outmode ? "outchunk" : "inchunk", nchunk); incount = 0; outcount = 0; starttime = clock(); if (outmode) { /* * Process the file, in constant output chunks. */ nsaved = 0; for (;;) { /* determine the amount of input needed */ nwanted = MAX(nchunk - nsaved, 0); ninput = GetMinInput(nwanted, inst); ASSERT(ninput <= inbufsize); /* read variable input */ nread = fread(inbuf, sizeof(short), ninput, infile); incount += nread; if (nread < ninput) break; /* not enough input */ /* resample, appending to saved output */ noutput = Resample(inbuf, nread, outbuf + nsaved, inst); outcount += noutput; ASSERT(noutput >= nwanted); ASSERT((nsaved + noutput) >= nchunk); ASSERT((nsaved + noutput) <= outbufsize); ASSERT(noutput <= GetMaxOutput(ninput, inst)); /* write constant output */ fwrite(outbuf, sizeof(short), nchunk, outfile); /* save extra output */ nsaved += noutput - nchunk; ASSERT(nsaved >= 0); memcpy(outbuf, outbuf + nchunk, nsaved * sizeof(short)); } /* resample the remaining input */ noutput = Resample(inbuf, nread, outbuf + nsaved, inst); outcount += noutput; /* write the remaining output */ fwrite(outbuf, sizeof(short), nsaved + noutput, outfile); } else { /* * Process the file, in constant input chunks. */ for (;;) { /* read constant input */ nread = fread(inbuf, sizeof(short), nchunk, infile); incount += nread; if (!nread) break; /* done */ /* resample */ noutput = Resample(inbuf, nread, outbuf, inst); outcount += noutput; /* write variable output */ fwrite(outbuf, sizeof(short), noutput, outfile); } } /* print timing info */ calctime = (clock() - starttime) / (float)CLOCKS_PER_SEC; audiotime = outcount / ((float)outrate * chans); printf("Processed %0.2fs of audio in %0.2fs ", audiotime, calctime); printf("[%0.2f%% realtime]\n", calctime * 100.0f / audiotime); FreeResampler(inst); free(inbuf); free(outbuf); fclose(infile); fclose(outfile); return 0; }