2016-07-22 7 views
0

C#で音量[]の音量を変更しようとしています。私はFFMPEGでサウンドファイルを読んで、その場で音量を変えたいと思っています。私はいくつかの例を見つけましたが、私はそれらを理解しませんでした。C#音量をどのように設定しますか[]

public void SendAudio(string pathOrUrl) 
{ 
    cancelVid = false; 
    isPlaying = true; 

    mProcess = Process.Start(new ProcessStartInfo 
    { // FFmpeg requireqs us to spawn a process and hook into its stdout, so we will create a Process 
     FileName = "ffmpeg", 
     Arguments = "-i " + (char)34 + pathOrUrl + (char)34 + // Here we provide a list of arguments to feed into FFmpeg. -i means the location of the file/URL it will read from 
     " -f s16le -ar 48000 -ac 2 pipe:1", // Next, we tell it to output 16-bit 48000Hz PCM, over 2 channels, to stdout. 
     UseShellExecute = false, 
     RedirectStandardOutput = true, // Capture the stdout of the process 
     Verb = "runas" 
    }); 

    while (!isRunning(mProcess)) { Task.Delay(1000); } 

    int blockSize = 3840; // The size of bytes to read per frame; 1920 for mono 
    byte[] buffer = new byte[blockSize]; 
    byte[] gainBuffer = new byte[blockSize]; 
    int byteCount; 

    while (true && !cancelVid) // Loop forever, so data will always be read 
    { 
     byteCount = mProcess.StandardOutput.BaseStream // Access the underlying MemoryStream from the stdout of FFmpeg 
     .Read(buffer, 0, blockSize); // Read stdout into the buffer 

     if (byteCount == 0) // FFmpeg did not output anything 
      break; // Break out of the while(true) loop, since there was nothing to read. 

     if (cancelVid) 
      break; 

     disAudioClient.Send(buffer, 0, byteCount); // Send our data to Discord 
    } 
    disAudioClient.Wait(); // Wait for the Voice Client to finish sending data, as ffMPEG may have already finished buffering out a song, and it is unsafe to return now. 
    isPlaying = false; 
    Console.Clear(); 
    Console.WriteLine("Done Playing!"); 
+0

を「私はいくつかの例を発見し、私はそれらを理解didntの。」あなたは何を理解していませんでしたか?コードをデバッグしようとしましたか? –

+0

彼らはちょうど物を変換し、それらを全く説明しませんでした。 – McLucario

+0

@cFrozenDeath投稿を無作為にdownvotingしていただきありがとうございます。今すぐこの質問に誰も答えません。 – McLucario

答えて

0

あなたの質問はあまりうまくいきません。イメージについて話していて、「イメージバイトで赤の量をどのように減らすか」と尋ねたとします。答えは、byte []を適切なビット深度のRGBタプルにデコードし、R値を変更してからバイト[]に変換する必要があるということです。同じことがここにあります。バイト[]を適切なビット深度のサンプルに変換し、再スケールし、バイト[]に変換し直します。

サンプルあたり16ビットがあるので、連続したバイトの各ペアを短くし、それをスケーリングし、次に分割する必要があります。

あなたはバイト[]読んでどこ最低料金:

for (int i = 0 ; i < blockSize/2 ; ++i) 
{ 

    // convert to 16-bit 
    short sample = (short)((buffer[i*2+1] << 8) | buffer[i*2]) 

    // scale 
    const double gain = 0.5; // value between 0 and 1.0 
    sample = (short)(sample * gain + 0.5); 

    // back to byte[] 
    buffer[i*2+1] = (byte)(sample >> 8); 
    buffer[i*2] = (byte)(sample & 0xff); 
} 
+0

16ビットの48000Hz PCM 2チャンネルサウンドを使用しています。これはうまくいくはずですか? – McLucario

+0

はい、バイトはサンプルごとにグループ化されています – jaket

関連する問題