2014-07-13 1 views
0

나는이 대답을 기반으로 WAV 파일에서 파형을 그릴하려고 해요 : https://stackoverflow.com/a/1215472/356635데이터를 파형을 끌기 위해 노력하지만 그리기하지

불행하게도, 그것은 큰 검은 사각형마다 시간을 생산하고 있습니다. 나는 이것이 왜 문제인지를 알아 내는데 어려움을 겪고있다. normalisedData의 모든 값은 -1 and +1 사이입니다. test.wav 파일은 5 초 알람 소리를 재생하는 Windows wav 파일입니다.

여기 내 코드입니다 : 코드가 가정보다

using System; 
using System.Drawing; 
using System.IO; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     const string rootDir = "C:\\inetpub\\wwwroot\\"; 
     if (File.Exists(rootDir + "test.png")) 
     { 
      File.Delete(rootDir + "test.png"); 
     } 

     var stream = new MemoryStream(File.ReadAllBytes(rootDir + "test.wav")); 
     var data = stream.ToArray(); 
     stream.Dispose(); 
     var normalisedData = FloatArrayFromByteArray(data); 

     // Get max value in data 
     var maxValue = 0f; 
     for (var i = 0; i < normalisedData.Length; i++) 
     { 
      if (normalisedData[i] > maxValue) maxValue = normalisedData[i]; 
     } 

     // Normalise data 
     for (var i = 0; i < normalisedData.Length; i++) 
     { 
      normalisedData[i] = (data[i]/maxValue) * 100f; 
     }  

     // Save picture 
     var picture = DrawNormalizedAudio(normalisedData, Color.LimeGreen); 
     picture.Save(rootDir + "test.png", System.Drawing.Imaging.ImageFormat.Png); 
     picture.Dispose(); 
    } 
    public static Bitmap DrawNormalizedAudio(float[] data, Color color) 
    { 
     Bitmap bmp; 
     bmp = new Bitmap(500,500); 

     int BORDER_WIDTH = 5; 
     int width = bmp.Width - (2 * BORDER_WIDTH); 
     int height = bmp.Height - (2 * BORDER_WIDTH); 

     using (Graphics g = Graphics.FromImage(bmp)) 
     { 
      g.Clear(Color.Black); 
      Pen pen = new Pen(color); 
      int size = data.Length; 
      for (int iPixel = 0; iPixel < width; iPixel++) 
      { 
       // determine start and end points within WAV 
       int start = (int)((float)iPixel * ((float)size/(float)width)); 
       int end = (int)((float)(iPixel + 1) * ((float)size/(float)width)); 
       float min = float.MaxValue; 
       float max = float.MinValue; 
       for (int i = start; i < end; i++) 
       { 
        float val = data[i]; 
        min = val < min ? val : min; 
        max = val > max ? val : max; 
       } 
       int yMax = BORDER_WIDTH + height - (int)((max + 1) * .5 * height); 
       int yMin = BORDER_WIDTH + height - (int)((min + 1) * .5 * height); 
       g.DrawLine(pen, iPixel + BORDER_WIDTH, yMax, 
        iPixel + BORDER_WIDTH, yMin); 
      } 
     } 
     return bmp; 
    } 

    public float[] FloatArrayFromByteArray(byte[] input) 
    { 
     float[] output = new float[input.Length/4]; 
     System.Buffer.BlockCopy(input, 0, output, 0, input.Length); 
     return output; 
    } 
} 
+0

파일 헤더를 고려해야합니까? –

+0

아, 그걸 설명 할 수 있어요! –

답변

1

WAV 파일이 더 복잡한 형식입니다. 헤더에는 오디오를 설명하는 기타 메타 데이터가 있습니다. 오디오 데이터는 일반적으로 부호가있는 16 비트 또는 8 비트 정수 값으로 저장되지만 다른 형식도 가능합니다. WAV 파일을 응용 프로그램에서 사용할 수있는 부동 소수점 값으로 디코딩 할 라이브러리를 찾거나 WAV 파일을 원시 부동 소수점 형식 파일로 변환하는 유틸리티를 찾아야합니다.

정규화 코드도 올바르지 않습니다. 샘플 값은 음수가 될 수 있으므로 샘플의 절대 값의 최대 값을 찾아야합니다. -100,100의 범위에서 정규화되므로 100을 곱하면 안됩니다.

관련 문제