Advertisement
Guest User

pitched down when converting stereo to mono

a guest
Jun 17th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. #define INBUF_SIZE 4096
  2. #define AUDIO_INBUF_SIZE 1152
  3. #define AUDIO_REFILL_THRESH 4096
  4.  
  5. OSStatus compressWithFF(NSString* source, NSString* dest, long bitRate, int channels);
  6.  
  7. OSStatus compressWithFF(NSString* source, NSString* dest, long bitRate, int channels){
  8.  
  9. OSStatus error = noErr;
  10.  
  11. AVCodec *codec;
  12. AVCodecContext *c= NULL;
  13. int frame_size, outbuf_size, ret;
  14. FILE *outfile, *f;
  15. uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  16. short *samples;
  17.  
  18. uint8_t *outbuf;
  19. AVPacket avpkt;
  20. AVFrame *encoded_frame = NULL;
  21. int got_packet_ptr = 0;
  22.  
  23. long progresspoint;
  24.  
  25. ExtAudioFileRef sourceFile = 0;
  26. CFURLRef sourceURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)source, kCFURLPOSIXPathStyle, false);
  27.  
  28. XThrowIfError(ExtAudioFileOpenURL(sourceURL, &sourceFile), "ExtAudioFileOpenURL failed");
  29.  
  30. UInt64 fileLength;
  31.  
  32. UInt32 flsize = sizeof(fileLength);
  33.  
  34.  
  35.  
  36.  
  37.  
  38. //get da size
  39. ExtAudioFileGetProperty(sourceFile, kExtAudioFileProperty_FileLengthFrames, &flsize, &fileLength);
  40. NSLog(@"--> file length in frames is %llu", fileLength);
  41.  
  42.  
  43.  
  44. av_init_packet(&avpkt);
  45. printf("Audio encoding\n");
  46. avcodec_register_all();
  47.  
  48.  
  49. codec = avcodec_find_encoder(CODEC_ID_MP3);
  50. if (!codec) {
  51.  
  52. fprintf(stderr, "codec not found\n");
  53. exit(1);
  54. }
  55.  
  56. c = avcodec_alloc_context3(codec);
  57. /* put sample parameters */
  58. c->bit_rate = bitRate;
  59. c->sample_rate = 44100;
  60. c->channels = channels;
  61. c->sample_fmt = AV_SAMPLE_FMT_S16;
  62. c->request_channels = 1;
  63.  
  64. c->channel_layout = AV_CH_LAYOUT_MONO;
  65.  
  66.  
  67. /* open it */
  68. if (avcodec_open2(c, codec, 0) < 0) {
  69. fprintf(stderr, "could not open codec\n");
  70. exit(1);
  71. }
  72.  
  73. f = fopen([source UTF8String], "rb");
  74. if (!f) {
  75. fprintf(stderr, "could not open %s\n", "mf");
  76. exit(1);
  77. }
  78.  
  79. fseek(f, 10, SEEK_SET);
  80.  
  81.  
  82. outfile = fopen([dest UTF8String], "wb");
  83. if (!outfile) {
  84. //fprintf(stderr, "could not open %s\n", outfilename);
  85. }
  86.  
  87.  
  88.  
  89. /* the codec gives us the frame size, in samples this is always 1152 methinks... */
  90. frame_size = c->frame_size;
  91.  
  92. //were going to need to set aside 1100 sampels times 2 bytes (16 bit) STEREO
  93. samples = (short*)malloc(frame_size * 2 * c->channels);
  94. outbuf_size = 3000;
  95. outbuf = (uint8_t*)malloc(outbuf_size);
  96.  
  97. /* encode until eof */
  98. avpkt.data = outbuf;
  99. // read into inbuff objects of size 1 byte , audio inbuf size of them, from f
  100.  
  101. avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  102. NSLog(@"just red a thousand bytes into inbuff from the original file file");
  103.  
  104. avpkt.size /= 2;
  105.  
  106. while (avpkt.size > 0) {
  107.  
  108. int got_frame = 0;
  109. if (!encoded_frame) {
  110. if (!(encoded_frame = avcodec_alloc_frame())) {
  111. fprintf(stderr, "out of memory\n");
  112. exit(1);
  113. }
  114. } else {
  115. avcodec_get_frame_defaults(encoded_frame);
  116.  
  117.  
  118.  
  119.  
  120. encoded_frame->nb_samples = AUDIO_INBUF_SIZE / (c->channels * av_get_bytes_per_sample(c->sample_fmt));
  121.  
  122.  
  123. //encoded_frame->nb_samples = frame_size;
  124.  
  125. if ((ret = avcodec_fill_audio_frame(encoded_frame, c->channels, c->sample_fmt,
  126. inbuf, AUDIO_INBUF_SIZE, 1)) < 0) {
  127. av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
  128. exit(1);
  129. }
  130.  
  131.  
  132. if (avcodec_encode_audio2(c, &avpkt, encoded_frame, &got_packet_ptr) < 0) {
  133. av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
  134. exit(1);
  135. }
  136. ret = avpkt.size;
  137.  
  138. if (got_packet_ptr) {
  139. /* if a frame has been encoded, output it */
  140.  
  141. int data_size = av_samples_get_buffer_size(NULL, c->channels,
  142. encoded_frame->nb_samples,
  143. c->sample_fmt, 1);
  144.  
  145. //fwrite(encoded_frame->data[0], 1, data_size, outfile);
  146.  
  147. fwrite(avpkt.data, 1, avpkt.size, outfile);
  148.  
  149. progresspoint += avpkt.size;
  150.  
  151. //NSLog(@"LOL------> %lu of %llu", progresspoint, fileLength);
  152.  
  153.  
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. float progress = (float)progresspoint / (float)fileLength;
  156.  
  157.  
  158.  
  159. NSNumber *prog = [NSNumber numberWithFloat:progress];
  160. NSString *title = [NSString stringWithString:@"converting"];
  161. NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
  162. [userInfo setObject:prog forKey:@"progress"];
  163. [userInfo setObject:title forKey:@"title"];
  164. [[NSNotificationCenter defaultCenter] postNotificationName:@"progress" object:title userInfo:userInfo];
  165. });
  166.  
  167.  
  168.  
  169. av_free_packet(&avpkt);
  170. }
  171.  
  172. avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
  173. avpkt.size = avpkt.size / 2;
  174. avpkt.dts =
  175. avpkt.pts = AV_NOPTS_VALUE;
  176.  
  177.  
  178.  
  179. #if 0
  180. if (avpkt.size < AUDIO_REFILL_THRESH) {
  181. /* Refill the input buffer, to avoid trying to decode
  182. * incomplete frames. Instead of this, one could also use
  183. * a parser, or use a proper container format through
  184. * libavformat. */
  185. memmove(inbuf, avpkt.data, avpkt.size);
  186. printf("memmove done\n");
  187. avpkt.data = inbuf;
  188. len = fread(avpkt.data + avpkt.size, 1,
  189. AUDIO_INBUF_SIZE - avpkt.size, f);
  190. printf("fread done\n");
  191. if (len > 0)
  192. avpkt.size += len;
  193. printf("refill done\n");
  194. }
  195.  
  196. #endif
  197.  
  198. }
  199. }
  200.  
  201. fclose(outfile);
  202. fclose(f);
  203.  
  204. avcodec_close(c);
  205. av_free(c);
  206. av_free(encoded_frame);
  207.  
  208. free(outbuf);
  209. free(samples);
  210.  
  211. return error;
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement