0

.opus ファイルを記録してから再生できる Android アプリ (Xamarin を使用) を作成しようとしています。Opus を使用したことがないので、ご容赦ください...

Concentus NuGet パッケージを使用してオーディオを圧縮/解凍しています。Concentus のサンプル コードは、記録のために次のことを提案しています。

// Initialize
OpusEncoder encoder = OpusEncoder.Create(48000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);
encoder.Bitrate = 12000;

// Encoding loop
short[] inputAudioSamples
byte[] outputBuffer[1000];
int frameSize = 960;

int thisPacketSize = encoder.Encode(inputAudioSamples, 0, frameSize, outputBuffer, 0, outputBuffer.Length); // this throws OpusException on a failure, rather than returning a negative number

この情報を使用して、次の記録方法を作成しました。

    private void RecordAudio(string filename)
    {
        _outputStream = File.Open(filename, FileMode.OpenOrCreate);
        _binaryWriter = new BinaryWriter(_outputStream);
        OpusEncoder encoder = OpusEncoder.Create(AudioFormat.SampleRate, 1, OpusApplication.OPUS_APPLICATION_AUDIO);
        encoder.Bitrate = 12000;

        int frameSize = AudioFormat.SampleRate * 20 / 1000;
        short[] audioBuffer = new short[frameSize];
        byte[] outputBuffer = new byte[1000];

        _audioRecorder = new AudioRecord(
          // Hardware source of recording.
          AudioSource.Mic,
          // Frequency
          AudioFormat.SampleRate,
          // Mono or stereo
          AudioFormat.ChannelIn,
          // Audio encoding
          AudioFormat.Encoding,
          // Length of the audio clip.
          audioBuffer.Length
        );
        _audioRecorder.StartRecording();

        _isRecording = true;
        while (_isRecording)
        {
            try
            {
                // Keep reading the buffer while there is audio input.
                _audioRecorder.Read(audioBuffer, 0, audioBuffer.Length);

                int thisPacketSize = encoder.Encode(audioBuffer, 0, frameSize, outputBuffer, 0, outputBuffer.Length); // this throws OpusException on a failure, rather than returning a negative number
                Debug.WriteLine("Packet size = " + thisPacketSize);
                // Write to the audio file.
                _binaryWriter.Write(outputBuffer, 0, thisPacketSize);
            }
            catch (System.Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
            }
        }
    }

を見るとthisPacketSize、可変であることがわかります。

Concentus は、デコード用に次のコードを提案しています。

OpusDecoder decoder = OpusDecoder.Create(48000, 1);

// Decoding loop
byte[] compressedPacket;
int frameSize = 960; // must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically
short[] outputBuffer = new short[frameSize];

int thisFrameSize = _decoder.Decode(compressedPacket, 0, compressedPacket.Length, outputBuffer, 0, frameSize, false);

再生に関する私の最初のアイデアは次のとおりです。

    void PlayAudioTrack(string filename)
    {
        _inputStream = File.OpenRead(filename);
        _inputStream.Position = _recording.CurrentPosition - _recording.CurrentPosition % AudioFormat.BytesPerSample;
        OpusDecoder decoder = OpusDecoder.Create(AudioFormat.SampleRate, 1);

        int frameSize = AudioFormat.SampleRate * 20 / 1000;

        _audioTrack = new AudioTrack(
          // Stream type
          Android.Media.Stream.Music,
          // Frequency
          AudioFormat.SampleRate,
          // Mono or stereo
          AudioFormat.ChannelOut,
          // Audio encoding
          AudioFormat.Encoding,
          // Length of the audio clip.
          frameSize,
          // Mode. Stream or static.
          AudioTrackMode.Stream);

        _audioTrack.SetPositionNotificationPeriod(AudioFormat.SampleRate / 30);
        _audioTrack.PeriodicNotification += _audioTrack_PeriodicNotification;
        _audioTrack.Play();

        short[] audioBuffer = new short[frameSize];

        int bytesRead = 0;
        do
        {
            try
            {
                byte[] compressedPacket = new byte[???];
                bytesRead = _inputStream.Read(compressedPacket, 0, compressedPacket.Length);
                int thisFrameSize = decoder.Decode(compressedPacket, 0, compressedPacket.Length, audioBuffer, 0, frameSize, false);
                Debug.WriteLine("Frame size = " + thisFrameSize);
                _audioTrack.Write(audioBuffer, 0, bytesRead);
            }
            catch (System.Exception)
            {
                bytesRead = 0;
            }
        } while (bytesRead > 0 && _audioTrack.PlayState == PlayState.Playing);

        if (!(_audioTrack.PlayState == PlayState.Stopped))
        {
            RaisePlaybackCompleted(this, new EventArgs());
        }
    }

compressedPacket私の問題は...フレームサイズが圧縮中に使用されたものと同じでなければならない場合、正しいフレームサイズに解凍するために必要なサイズをどのように判断できますか(私が正しく理解している場合)?

4

1 に答える 1

3

あなたが求めているのは、パケット化メカニズムだと思います。ここでの本当の問題は、Concentus がこれまで生の Opus パケットでのみ動作し、パケットが入っているコンテナーを実際に解析するコードがないことです (.opus ファイルは、実際には opus データ ストリームを含む Ogg メディア コンテナー ファイルです)。したがって、.opus フォーマットを実際に読み書きしたい場合は、Ogg ファイル用のリーダー/ライターを実装するか、私が Concentus パッケージに実際に実装するのを待つ必要があります (申し訳ありません)。

NVorbis には、目的に合わせて変更できるネイティブの Ogg パーサーがあります。または、他のプログラムとの互換性を気にしない場合は、エンコードされた Opus データの各ブロックの前に 2 バイトのパケット長フィールドを書き込むことで、独自の基本的なパケタイザーを実装できます。

編集: とにかくそれに到達する必要があったので、Oggfile パーサー パッケージの作業を開始しましたが、これまでのところデコーダーのみが実装されています: https://www.nuget.org/packages/Concentus.Oggfile

edit2: Opusfile エンコーダーが実装されました ;-)

于 2016-10-01T19:07:44.420 に答える