2014-01-23 6 views
0

마이크에서 오디오를 녹음하고 base64 문자열로 변환 한 다음 서버로 보내려고합니다.서버에 마이크의 오디오 보내기

그런 다음 서버는 base64 문자열을 .wav 파일로 변환합니다.

을 내 C# 코드 :

IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(Filename, FileMode.Create, myIsolatedStorage); 
    fileStream.Write(stream.GetBuffer(), 0, (int)stream.Position); 

    fileStream.Position = 0; 



     // Convert to base64 string and then urlencode it: 

byte[] binaryData = new Byte[fileStream.Length]; 
long bytesRead = fileStream.Read(binaryData, 0, (int)fileStream.Length); 
string fileBase64 = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);    
fileBase64 = HttpUtility.UrlEncode(fileBase64); 



     // Send it to server: 


     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://.../upload.php"); 
     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 

     string postData = String.Format("file={0}", fileBase64); 

     // Getting the request stream. 
     request.BeginGetRequestStream 
      (result => 
      { 
       // Sending the request. 
       using (var requestStream = request.EndGetRequestStream(result)) 
       { 
        using (StreamWriter writer = new StreamWriter(requestStream)) 
        { 
         writer.Write(postData); 
         writer.Flush(); 
        } 
       } 

       // Getting the response. 
       request.BeginGetResponse(responseResult => 
       { 
        var webResponse = request.EndGetResponse(responseResult); 
        using (var responseStream = webResponse.GetResponseStream()) 
        { 
         using (var streamReader = new StreamReader(responseStream)) 
         { 
          string srresult = streamReader.ReadToEnd(); 
         } 
        } 
       }, null); 
      }, null); 

내 PHP 스크립트는 "+/8cADkAOQAWAPD/7 층/5 ..."다음 base64_decode로 .wav 파일로 변환 PHP와 같은 base64로 문자열을받을 기능. 그러나이 파일을 VLC로 열면 아무 것도 재현하지 못합니다.

byte[] buffer = new byte[microphone.GetSampleSizeInBytes(duration)]; 
using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication()) 
using (IsolatedStorageFileStream openfilestream = userStore.OpenFile(Filename, FileMode.Open)) 
      { 
       openfilestream.Read(buffer, 0, buffer.Length); 
      } 

    SoundEffect sound = new SoundEffect(buffer, microphone.SampleRate, AudioChannels.Mono); 
    soundInstance = sound.CreateInstance(); 
    soundIsPlaying = true; 
    soundInstance.Play(); 

누군가가 나를 도울 수 :이 방법으로 파일을 열 대신하는 경우

내가 저장 한 직후, 다음 사운드를 재현, (아래 참조)?

+0

원시 오디오 데이터를 저장하고 SoundEffect로 재생하고 있습니다. WAVE 파일은 단순히 데이터가 아니지만 헤더도 있습니다 (비트 맵 파일은 일련의 픽셀이 아니므로). 프로그램으로 재생하려면 헤더 (Google for specs)로 저장해야합니다. 운이 좋으면 단순한 파일 형식이고 헤더는 바이트 (형식, 주파수, 채널) 일뿐입니다. –

답변

1

원시 오디오 데이터를 저장하고 있습니다 (그리고 SoundEffect).

WAVE 파일은 데이터가 아니지만 헤더도 있습니다 (비트 맵 파일은 일련의 픽셀이 아니므로). 프로그램으로 재생하려면 헤더로 저장해야합니다 (specifications 참조).

당신은 같은 것을 할 수 있도록 매우 쉬운 형식의 운이 좋다 (검증되지 않은 단지 설명을 위해 꽤 원료를, 당신은 당신이 WAVE 원시 스트림을 변환 할 때 그것을 서버 측을해야 할) :

// First 4 bytes are file format marker. Container file format 
// is RIFF (it's a tagged file format) 
streamWriter.Write(Encoding.ASCII.GetBytes("RIFF")); 

// Number of bytes, header + audio samples 
streamWriter.Write(36 + sampleCount * channelCount * samplingRate); 

// Beginning of chunk specific of WAVE files, it describe how 
// data are stored 
streamWriter.Write(Encoding.ASCII.GetBytes("WAVEfmt ")); 
streamWriter.Write(16); // It's always 16 bytes 

// Audio stream is PCM (value 1) 
streamWriter.Write((UInt16)1); 

// Player will use these information to understand how samples 
// are stored in the stream. 
streamWriter.Write(channelCount); 
streamWriter.Write(samplingRate); 
streamWriter.Write(samplingRate * bytesPerSample * channelCount); 
streamWriter.Write(bytesPerSample * channelCount); 
streamWriter.Write((UInt16)(8 * bytesPerSample)); 

// Now the chunk that contains audio stream, just add its marker 
// and its length then write all your samples (in the raw format you have) 
streamWriter.Write(Encoding.ASCII.GetBytes("data")); 
streamWriter.Write(sampleCount * bytesPerSample); 
+0

channelcount, samplingRate, sampleCount, ecc는 무엇입니까? – xRobot

+0

이름에서 알 수 있듯이 : 획득 한 스트림의 채널 수 (1 = 모노, 2 = 스테레오); samplingRate ... 초당 샘플 수가 많음. sampleCount = 획득 한 샘플 수 (= duration * seconds * samplingRate * channelCount) –

+0

및 녹음 된 오디오 파일에 대해 samplingRate가 무엇인지 어떻게 알 수 있습니까? 고마워요 :) – xRobot