2

Android で soundtouch (オープン ソースのオーディオ処理ライブラリ) を統合するテスト アプリに取り組んでいます。

私のテスト アプリは、既にマイクから入力を受け取り、soundtouch を介してオーディオを渡し、処理されたオーディオを AudioTrack インスタンスに出力することができます。

私の質問は、AudioTrack からの出力をデバイス上の新しいファイルに変更するにはどうすればよいですか?

これが私のアプリの関連コードです(ここで、soundtouchの出力をAudioTrackの入力に処理しています)

// the following code is a stripped down version of my code
// in no way its supposed to compile or work.  Its here for reference purposes
// pre-conditions 
// parameters -  input : byte[]
soundTouchJNIInstance.putButes(input);
int bytesReceived = soundTouchJNIInstance.getBytes(input);
audioTrackInstance.write(input, 0, bytesReceived);

この問題にアプローチする方法についてのアイデアはありますか? ありがとう!

4

6 に答える 6

0

これを byteArray に書き込む最良の方法:

public void writeToFile(byte[] array) 
{ 
    try 
    { 
        String path = "Your path.mp3"; 
        File file = new File(path);
        if (!file.exists()) {
        file.createNewFile();
        }

        FileOutputStream stream = new FileOutputStream(path); 
        stream.write(array); 
    } catch (FileNotFoundException e1) 
    { 
        e1.printStackTrace(); 
    } 
} 
于 2014-04-04T07:49:12.130 に答える
0

SequenceInputStream を使用してメソッドを使用できます。私のアプリでは、MP3 ファイルを 1 つにマージし、JNI ライブラリ MPG123 を使用して再生するだけですが、MediaPlayer を使用して問題なくファイルをテストしました。

このコードは最高ではありませんが、機能します...

private void mergeSongs(File mergedFile,File...mp3Files){
        FileInputStream fisToFinal = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(mergedFile);
            fisToFinal = new FileInputStream(mergedFile);
            for(File mp3File:mp3Files){
                if(!mp3File.exists())
                    continue;
                FileInputStream fisSong = new FileInputStream(mp3File);
                SequenceInputStream sis = new SequenceInputStream(fisToFinal, fisSong);
                byte[] buf = new byte[1024];
                try {
                    for (int readNum; (readNum = fisSong.read(buf)) != -1;)
                        fos.write(buf, 0, readNum);
                } finally {
                    if(fisSong!=null){
                        fisSong.close();
                    }
                    if(sis!=null){
                        sis.close();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fos!=null){
                    fos.flush();
                    fos.close();
                }
                if(fisToFinal!=null){
                    fisToFinal.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 
于 2014-04-09T17:23:03.010 に答える
0

すでにマイクから入力音声を取得してファイルに保存していることを願っています。

まず、JNI ライブラリを oncreate メソッドにインポートします。

System.loadLibrary("soundtouch");
System.loadLibrary("soundstretch");

サウンドストレッチ ライブラリ :

public class AndroidJNI extends SoundStretch{
    public final static SoundStretch soundStretch = new SoundStretch();
}

ここで、入力ファイル パスと目的の出力ファイルを使用して soundstrech.process を呼び出し、処理された音声をパラメーターとして保存する必要があります。

AndroidJNI.soundStretch.process(dataPath + "inputFile.wav", dataPath + "outputFile.wav", tempo, pitch, rate);
        File sound = new File(dataPath + "outputFile.wav");
        File sound2 = new File(dataPath + "inputFile.wav");       
        Uri soundUri = Uri.fromFile(sound);

soundUri は、再生用のメディア プレーヤーへのソースとして提供できます。

MediaPlayer mediaPlayer = MediaPlayer.create(this, soundUri);
        mediaPlayer.start();

また、記録用のサンプル サイズは、Array of Sample Rates を宣言して動的に選択する必要があることに注意してください。

int[] sampleRates = { 44100, 22050, 11025, 8000 }
于 2014-04-04T03:49:02.427 に答える
0

簡単なテスト コード スニペットを使用して、オーディオ バイト配列を記述します。

public void saveAudio(byte[] array, string pathAndName)
{
  FileOutputStream stream = new FileOutputStream(pathAndName);
  try {
      stream.write(array);
  } finally {
      stream.close();
  }
}

これを実稼働環境で使用する場合は、おそらくいくつかの例外処理を追加する必要がありますが、開発段階または個人的な非リリース プロジェクトでは常に、上記を使用してオーディオを保存します。

補遺

少し考えた後、スニペットを次のやや堅牢な形式に変更しました。

public void saveAudio(byte[] array, string pathAndName)
{
  try (FileOutputStream stream= new FileOutputStream(pathAndName)) {
      stream.write(array);
  } catch (IOException ioe) {
      ioe.printStackTrace();
  } finally {
      stream.close();
  }
}
于 2014-04-09T09:15:05.593 に答える
0

byte[]これを達成する最善の方法は、そのオーディオを配列に変換することだと思います。すでにそれを行っていると仮定すると (そうでない場合は、コメントしてください。例を示します)、上記のコードは機能するはずです。これは、外部sdcardの という名前の新しいディレクトリAudioRecordingに保存し、audiofile.mp3.

final File soundFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "AudioRecording/");
soundFile.mkdirs();
final File outFile = new File(soundFile, 'audiofile.mp3');

try {
  final FileOutputStream output = new FileOutputStream(outFile);
  output.write(yourByteArrayWithYourAudioFileConverted);
  output.close();
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

mkdirs()親ディレクトリが見つからない場合、メソッドはすべての親ディレクトリを作成しようとします。したがって、2 レベル以上の深さのディレクトリに格納する予定がある場合、これによりすべての構造が作成されます。

于 2014-04-02T19:07:01.510 に答える