2011-01-23 26 views
4

オーディオを録音し、サンプルをSDカードに保存してから、レコードボタンと再生ボタンを使用して再生します。私はこのサンプルを逆にする必要があります。私はこれをすべて行うことができ、逆のサンプルは別の名前でSDカードに保存されます。元のサンプルはtest.wavで、逆のサンプルはrevFile.wavとして保存されます。私が試してみると、revFile.wavアンドロイドはこのフォーマットを再生できないと言います。MediaPlayerで反転した.wavファイルを再生できません

サンプルを静的に配列に入れてから内容を逆にしました。最初にストライピングが必要なサンプルの先頭にヘッダ情報がある可能性があります。ありがとう。

これまで私がこれまで持っていたことは次のとおりです。

public class recorder extends Activity { 
    MediaRecorder myRecorder = null; 
    DataInputStream dis = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
    public void onClickPlay(View v){ 
     Log.v("onClickplay", "play clicked"); 

     try{ 
      MediaPlayer mp = new MediaPlayer(); 
      mp.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/test.wav"); 
      mp.prepare(); 
      mp.start(); 
     } catch(Exception e3) { 
      e3.printStackTrace(); 
     } 
     TextView text = (TextView)findViewById(R.id.TextView01); 
     text.setText("playing"); 
    } 

    public void onClickRecord(View v){ 
     Log.v("onClickRecord", "record clicked"); 

     File path = Environment.getExternalStorageDirectory(); 
     Log.v("file path", ""+path.getAbsolutePath()); 

     File file = new File(path, "test.wav"); 
     if(file.exists()){ 
      file.delete(); 
     } 
     path.mkdirs(); 
     Log.v("file path", ""+file.getAbsolutePath()); 
     myRecorder = new MediaRecorder(); 
     myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

     myRecorder.setOutputFile(file.getAbsolutePath()); 
     Log.i("myrecorder", "about to prepare recording"); 
     try{ 
      myRecorder.prepare(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 

     Log.i("myrecorder", "prepared"); 
     myRecorder.start(); // Recording is now started 
     Log.i("myrecorder", "recording"); 
     TextView text = (TextView)findViewById(R.id.TextView01); 
     text.setText("recording"); 
    } 

    public void onClickStop(View v){ 
     Log.v("onClickStop", "stop clicked"); 

      try{ 
      myRecorder.stop(); 
      myRecorder.reset(); // You can reuse the object by going back to setAudioSource() step 
      myRecorder.release(); // Now the object cannot be reused 
      }catch(Exception e){} 

      TextView text = (TextView)findViewById(R.id.TextView01); 
      text.setText("recording stopped"); 

     }  



public void onClickReverse(View v){ 
     Log.v("onClickReverse", "reverse clicked"); 

     File f = Environment.getExternalStorageDirectory(); 
     String path = f.getAbsolutePath(); 
     path = path + "/test.wav"; 
     Log.v("path = ", ""+path); 
     Log.v("dir = ", ""+f.getAbsolutePath()); 
     Log.v("test file exists? = ", ""+f.getAbsolutePath()+"/test.wav"); 

     File f2 = new File(path); 
     Log.v("f2 = ", ""+f2.getAbsolutePath()); 

     try { 
      InputStream is = new FileInputStream(f2); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      dis = new DataInputStream(bis); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     int fileLength = (int)f2.length(); 
     byte[] buffer = new byte[fileLength]; 

     /*File reversedFile = Environment.getExternalStorageDirectory(); 
      File revFile = new File(reversedFile, "reversedFile.wav"); 
      Log.v("reversedfile path", ""+ revFile.getAbsolutePath()); 

      if(revFile.exists()){ 
       revFile.delete(); 
      } 

      reversedFile.mkdirs(); 
    */ 

     byte[] byteArray = new byte[fileLength +1]; 
     Log.v("bytearray size = ", ""+byteArray.length); 

     try { 
      while(dis.read(buffer) != -1) { 
       dis.read(buffer); 
       Log.v("about to read buffer", "buffer"); 

       byteArray = buffer; 
      } 

      Log.v(" buffer size = ", ""+ buffer.length); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

      byte[] tempArray = new byte[fileLength]; 

      int j=0; 
      for (int i=byteArray.length-1; i >=0; i--) { 
       tempArray[ j++ ] = byteArray[i]; 
      } 

      File revPath = Environment.getExternalStorageDirectory(); 
      Log.v("revpath path", ""+revPath.getAbsolutePath()); 

      File revFile = new File(revPath, "revFile.wav"); 
      Log.v("revfile path ", ""+revFile.getAbsolutePath()); 
      if(revFile.exists()){ 
       revFile.delete(); 
      } 

      revPath.mkdirs(); 

      try { 
       OutputStream os = new FileOutputStream(revFile); 
       BufferedOutputStream bos = new BufferedOutputStream(os); 
       DataOutputStream dos = new DataOutputStream(bos); 
       Log.v("temparray size = ", ""+ tempArray.length); 
       dos.write(tempArray); 
       dos.flush(); 
       dos.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      try{ 
       MediaPlayer mp = new MediaPlayer(); 
       mp.setDataSource(Environment.getExternalStorageDirectory().getPath() 
                    +"/revFile.wav"); 

       mp.prepare(); 
       mp.start(); 
     } catch(Exception e3) { 
       e3.printStackTrace(); 
     } 
      TextView text = (TextView)findViewById(R.id.TextView01); 
      text.setText("playing reversed file"); 
     } 

}// end of onclickrev 

答えて

3

link text

で.WAVヘッダの良好な記述がある。また、ファイル内のすべてのデータをリトルエンディアン順(1つのマルチバイト番号の下位バイトを最下位アドレスに格納されているように記憶されています。 ...)ので、バイトを逆順にすることはできません。各サンプルが何バイトであるかを調べる(通常は16ですが、ヘッダーをチェックする)必要があります。

+1

リンクに感謝しました。 – turtleboy

1

私はあなたが正しいと問題はあなたがヘッダ情報を破壊しているので、あなただけの波形を反転させるために、ファイル内のバイトを逆にすることができないということであるということをかなり確信しています。 .wavファイルでの作業経験が少ないので、これを行うには良いライブラリがあるかどうかを調べるべきです。

+0

okありがとう、私はアンドロイドには新しく、以前はオーディオで作業していませんでしたが、最初のバイトのどれだけがヘッダーであるかを見つけ出すだけではありません。私は、配列の残りの部分を反転し、先頭にヘッダーバイトを追加することができますか? – turtleboy

3

WAVファイルフォーマットには44バイトのヘッダーチャンクが含まれています。ほとんどのWAVファイルはこの44バイトのヘッダーの後に実際のサンプルデータが続きます。したがって、WAVファイルをリバースするには、元のファイルから44バイトのヘッダーをコピーしてから、の後に元のの反転サンプルデータをコピーしてください。元のファイル全体のバイト順を逆順にするだけでは、まったく動作しません。また、ヘッダーをコピーして残りのファイルのバイト順序を逆にすると、実際には機能しません(実際にはの種類がになります)。実際にはフレームを逆にする必要があります。フレームサイズはサンプルあたりのバイト数とファイルがステレオかモノラルかに依存します(たとえば、ファイルがステレオでサンプルあたり2バイトの場合、各フレームは4バイト)。

このように、すべてのWAVファイルが「標準」であるとは限りません。 WAVファイルは実際にはRIFFファイルの変種であるため、技術的には元のファイル内のヘッダーとサンプルデータのさまざまな部分を見つけるためにはるかに複雑なコードが必要です。しかし、ほとんどのWAVファイルはヘッダーの後にサンプルが続きます(オーディオを自分で録音する場合は確かに当てはまります)。この場合、多くの作業を省くことができます。

Joe Cullityのリンクは、WAVファイル形式の説明です。

+0

ありがとう、これは大きな助けになります。 – turtleboy

関連する問題