xiph / libopusenc

Library for encoding .opus audio files and live streams.
BSD 3-Clause "New" or "Revised" License
108 stars 39 forks source link

the out file of opusenc_example is different with run opusenc #26

Closed zhengzyyyy closed 3 years ago

zhengzyyyy commented 3 years ago

I try to use opusenc for converting a pcm to ogg file, so I modified the sampling rate and the number of channels in the opusenc_example code to the real value of the pcm. And use the opusenc_example to make convert.

But the duration of this ogg file is half of the output of the tool, and it sounds faster than normal, Can anyone help me?

The format of pcm file: sampling rate: 8000 channels: 1

(1) ----------opusenc_example------------------

#include <stdio.h>
#include "opusenc.h"

#define READ_SIZE 256

int main(int argc, char **argv) {
  FILE *fin;
  OggOpusEnc *enc;
  OggOpusComments *comments;
  int error;
  if (argc != 3) {
    fprintf(stderr, "usage: %s <raw pcm input> <Ogg Opus output>\n", argv[0]);
    return 1;
  }
  fin = fopen(argv[1], "rb");
  if (!fin) {
    fprintf(stderr, "cannot open input file: %s\n", argv[1]);
    return 1;
  }
  comments = ope_comments_create();
  ope_comments_add(comments, "ARTIST", "Someone");
  ope_comments_add(comments, "TITLE", "Some track");
  enc = ope_encoder_create_file(argv[2], comments, **8000, 1, 0**, &error);
  if (!enc) {
    fprintf(stderr, "error encoding to file %s: %s\n", argv[2], ope_strerror(error));
    ope_comments_destroy(comments);
    fclose(fin);
    return 1;
  }
  while (1) {
    short buf[2*READ_SIZE];
    int ret = fread(buf, 2*sizeof(short), READ_SIZE, fin);
    if (ret > 0) {
      ope_encoder_write(enc, buf, ret);
    } else break;
  }
  ope_encoder_drain(enc);
  ope_encoder_destroy(enc);
  ope_comments_destroy(comments);
  fclose(fin);
  return 0;
}

./opusenc_example 8k16bits_1chan.pcm example_out.opus

(2) opusenc 8k16bits_1chan.pcm tool_out.opus --raw-rate 8000 --raw-chan 1 --raw

image image

here is the pcm file. pcm.zip

rillian commented 3 years ago

I didn't try to reproduce, but it looks like you're reading two 2 16-bit integers per sample, but your example filename says it's mono, not stereo. If you ask for 1 encoded channel with ope_encoder_create_* I'd expect that to just double to size of your input buffer. But if you ask for stereo and feed in mono data two samples at a time, that would explain the doubled playback speed. So maybe double-check that?

zhengzyyyy commented 3 years ago

I didn't try to reproduce, but it looks like you're reading two 2 16-bit integers per sample, but your example filename says it's mono, not stereo. If you ask for 1 encoded channel with ope_encoder_create_* I'd expect that to just double to size of your input buffer. But if you ask for stereo and feed in mono data two samples at a time, that would explain the doubled playback speed. So maybe double-check that?

Thank you rillian, you are right!

image

After I made the above modification, the conversion was normal.