How to amplify, increase bitrate and Fadin- fadout

2019-09-05 08:47发布

问题:

I am facing the following problem regarding to the voice (Audio) recording in windows phone 7.

I am recording the sound using Microphone class available in Microsoft.Xna.Framework.Audio namespace. here is the code -

Variable declaration:

private Microphone mic = Microphone.Default;
private MemoryStream stream;
private const string FILE_NAME = "recording.mp3";
byte[] buffer;

Recording Button click code-

mic.BufferDuration = TimeSpan.FromSeconds(1);
buffer = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];

// Create the event handler.  I could have done an anonymous   
// delegate here if I had so desired.  
mic.BufferReady += new EventHandler<EventArgs>(mic_BufferReady);

stream = new MemoryStream();
mic.Start();

Buffer Ready Event code ----------

void mic_BufferReady(object sender, EventArgs e)
{
    mic.GetData(buffer);
    // Write buffer to stream
    stream.Write(buffer, 0, buffer.Length);  
}

Button stop code -

private void btnStop_Click(object sender, RoutedEventArgs e)
{
    dt.Stop();
    btnStop.IsEnabled = false;
    btnPlayRecording.IsEnabled = true;

    mic.Stop();
    //Writing stream into Storage
     writeFile(stream);
}

private void writeFile(MemoryStream s, string name)
{
    try
    {
        using (var userStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (userStore.FileExists(name))
            {
                userStore.DeleteFile(name);
            }
            using (var file = userStore.OpenFile(name, FileMode.CreateNew))
            {
                s.WriteTo(file);
            }
        }
    }
    catch (Exception ee)
    {

    }
}

Once I save the stream into isolated storage and play it back, the volume is very low and the quality is also not good.

So

  1. Can we amplify the Volume ?
  2. can we increase the bitrate ?
  3. Can we do Fadin-Fadout ?

If all these three is not possible in windows phone 7, then is there any third party API available to perform all these operations?

Thanks in advance

回答1:

Amplification in .NET is not trivial. The best approach I found was to make an external process call to "SoX, the Swiss Army knife of sound processing programs." (http://sox.sourceforge.net/)

I don't have a Windows 7 phone -- so I don't know for sure that SOX runs there.

The format would be SOX -V volume_parameter inputFileName outputFileName for amplification, so:

"sox.exe -v 3.0 myNormalFile.wav myAmpedFile.wav"

would give you an amplification of 300%. Sox also allows for bitrate change... not sure of Fadein/Fadeout.

I don't have anything specifically for Windows 7 phone, but in straight .NET/C# Code:

string finalFileName = "myAmplifiedFile.WAV";
string tmpFileName = "tmpHoldingFile.WAV";
string soxEXE = @"C:\SOX\sox.exe";
string soxArgs = "-v 3.0 ";

/// OTHER STUFF HERE 

/*-----------------------------------------------------------
* Call the SOX utility to amplify it so it is 3 times as loud.
*-----------------------------------------------------------*/
try
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo = new System.Diagnostics.ProcessStartInfo();
    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process.StartInfo.FileName = soxEXE;
    process.StartInfo.Arguments = string.Format("{0} {1} {2}",
                             soxArgs, tmpFileName, finalFileName);
    process.Start();
    process.WaitForExit();
    int exitCode = process.ExitCode;
}
catch (Exception ex)
{
    string err = ex.Message;
    return false;
}
/*-------------------------------------------------------------
 * OK, now we play it using SoundPlayer
 *-------------------------------------------------------------*/
try
{
    SoundPlayer simpleSound = new SoundPlayer(@finalFileName);
    simpleSound.PlaySync();
    FileInfo readFile = new FileInfo(finalFileName);
    string finalDestination = finalDirectory + "/" + readFile.Name;
    readFile.MoveTo(finalDestination);
}
catch (Exception e)
{
    string errmsg = e.Message;
    return false;
}
finalFileName = "";
tmpFileName = "";
spVoice = null;