2016-07-22 4 views
0

Ich versuche, die Lautstärke von Sound-Bytes [] in C# zu ändern. Ich lese eine Audiodatei mit FFMPEG und möchte die Lautstärke im laufenden Betrieb ändern. Ich habe ein paar Beispiele gefunden, aber ich habe sie nicht verstanden.C# Wie stelle ich die Lautstärke von Sound-Bytes ein []

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

Was hast du nicht verstanden? Hast du versucht, den Code zu debuggen? –

+0

sie konvertieren nur Zeug und erklärten sie überhaupt nicht. – McLucario

+0

@cFrozenDeath Vielen Dank, dass Sie meinen Beitrag per Zufall abgelehnt haben. Nun beantwortet wohl niemand diese Frage. – McLucario

Antwort

0

Ihre Frage nicht sehr gut formuliert. Stellen Sie sich vor, Sie hätten über ein Bild gesprochen und gefragt: "Wie verringere ich die Menge an Rot in Bildbytes []?". Die Antwort wäre, dass Sie byte [] in ein RGB-Tupel mit der entsprechenden Bittiefe dekodieren, den R-Wert ändern und dann zurück in ein byte [] konvertieren müssen. Das selbe hier. Sie konvertieren das Byte [] in Samples der entsprechenden Bittiefe, rescale und konvertieren dann zurück in ein Byte [].

In Ihrem Fall gibt es 16 Bits pro Beispiel, so dass Sie jedes Paar aufeinanderfolgender Bytes zu einem kurzen verbinden, skalieren und dann wieder auseinander teilen müssen.

ab, wo Sie die byte [] lesen: „Ich habe einige Beispiele gefunden und aber ich nicht sie verstehen“

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

Ich verwende einen 16-Bit 48000Hz PCM 2-Kanal-Sound. Sollte das auch funktionieren? – McLucario

+0

Ja, die Bytes sind nach Beispiel gruppiert – jaket

Verwandte Themen