Advertisement
wandrake

Untitled

Mar 12th, 2012
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.28 KB | None | 0 0
  1. module Main
  2.  
  3. open System
  4. open System.Windows.Forms
  5. open System.Media
  6. open System.Drawing
  7. open System.IO
  8. open System.Timers
  9.  
  10. open MathNet.Numerics.Transformations
  11.  
  12. open LineChart
  13. open ContinueChart
  14. open WaveUtils
  15. open Charts
  16. open Utils
  17. open DSoundPlayer
  18.  
  19. open ViewStream
  20. [<EntryPointAttribute>]
  21. let main argv =
  22.     let refreshFrequency = 25
  23.     //let tick = 1000 / refreshFrequency
  24.     let tick = 1000. / double refreshFrequency
  25.     printfn "%f" tick
  26.  
  27.     let form = new Form(Text = "Chart test");
  28.  
  29.     let sampleChart = new ContinueChart()
  30.     let transformChart = new ContinueChart()
  31.  
  32.     sampleChart.Width <- 500
  33.     sampleChart.Height <- 200
  34.  
  35.     (* sampleChart.XFix <- XFix.Middle *)
  36.     (* sampleChart.YFix <- YFix.Left *)
  37.  
  38.     transformChart.Width <- 500
  39.     transformChart.Height <- 200
  40.     transformChart.Top <- 200
  41.    
  42.     transformChart.XFix <- XFix.Bottom
  43.     transformChart.YFix <- YFix.Left
  44.  
  45.     let mutable phase = 0.
  46.     let timer = new Timer()
  47.     timer.Interval <- tick
  48.  
  49.     let _sampleRate = 48000
  50.     let fileStream = new FileStream(argv.[0], FileMode.Open)
  51.  
  52.     let printb (buffer: byte array) =
  53.         for i in 0..(buffer.Length-1) do
  54.             printf "0x%2x " buffer.[i]
  55.         printfn ""
  56.  
  57.     let soundPlayer = new SoundPlayer()
  58.  
  59.     let buffer: byte[] = Array.zeroCreate((2 * _sampleRate) / refreshFrequency)
  60.     let streamReader = new BinaryReader(fileStream)
  61.  
  62.     let rec printListP (list: PointF list) =
  63.             match list with
  64.             | [] -> printfn ""
  65.             | a::b ->
  66.                 printfn "(%f, %f) " (a.X) (a.Y)
  67.                 printListP b
  68.  
  69.     let rec printListF (list: float list) =
  70.             match list with
  71.             | [] -> printfn ""
  72.             | a::b ->
  73.                 printf "%.2f," a
  74.                 printListF b
  75.  
  76.     let rec printListI (list: int list) =
  77.             match list with
  78.             | [] -> printfn ""
  79.             | a::b ->
  80.                 printf "%d," a
  81.                 printListI b
  82.  
  83.     let rft = new RealFourierTransformation()
  84.     transformChart.AutoFix <- false
  85.     let soundStream = new MemoryStream()
  86.     let counter = ref 0
  87.  
  88.     timer.Elapsed.Add(fun _ ->
  89.         let x = streamReader.Read(buffer, 0, buffer.Length)
  90.  
  91.         let bufferList = Array.toList(buffer)
  92.         let values: float[] = ArrayFromPCM16 buffer
  93.         let chartPoints = PointsFromArray(values, _sampleRate)
  94.  
  95.         let rTransf, cTransf = (ref null, ref null)
  96.         let sizeNew = nextTwoPower values.Length
  97.         //printfn "%d %d" values.Length sizeNew
  98.         let trValues = Array.zeroCreate sizeNew
  99.  
  100.         for i in 0..(values.Length-1) do
  101.             trValues.[i] <- values.[i]
  102.  
  103.         rft.TransformForward(trValues, rTransf, cTransf)
  104.    
  105.         let transfPoints = PointsFromArray(!rTransf, 44100)
  106.        
  107.         sampleChart.newData(chartPoints)
  108.         //sampleChart.Color <- Color.FromArgb(25 * (!counter%10), 25 * (!counter%10), 0)
  109.         transformChart.newData(transfPoints)
  110.  
  111.         if x < buffer.Length then timer.Stop()
  112.  
  113.         counter := !counter + 1
  114.     )
  115.  
  116.     soundPlayer.Stream <- fileStream
  117.     soundPlayer.Play()
  118.  
  119.     streamReader.BaseStream.Seek(0L, SeekOrigin.Begin) |> ignore
  120.     timer.Start()
  121.  
  122.     let mutable count = 0
  123.  
  124.     let x = new PointF(3.f, 2.f)
  125.     let upButton = new Button()
  126.     let downButton = new Button()
  127.  
  128.     upButton.Click.Add(fun _ -> sampleChart.UpScale())
  129.     downButton.Click.Add(fun _ -> sampleChart.DownScale())
  130.  
  131.     let tablePanel = new TableLayoutPanel()
  132.  
  133.     tablePanel.RowCount <- 2
  134.     tablePanel.ColumnCount <- 1
  135.     let panel = new Panel()
  136.  
  137.     upButton.Text <- "↑"
  138.     upButton.Dock <- DockStyle.Left
  139.     upButton.Height <- 20
  140.  
  141.     downButton.Text <- "↓"
  142.     downButton.Dock <- DockStyle.Right
  143.     downButton.Height <- 20
  144.  
  145.     panel.Controls.Add(upButton)
  146.     panel.Controls.Add(downButton)
  147.     panel.Height <- 20
  148.     tablePanel.Controls.Add(sampleChart, 0, 0)
  149.     tablePanel.Controls.Add(transformChart, 1, 0)
  150.     tablePanel.Controls.Add(panel, 2, 0)
  151.     tablePanel.Dock <- DockStyle.Fill
  152.     form.Controls.Add(tablePanel)
  153.     form.Controls.Add(transformChart)
  154.     form.Width <- 500
  155.     form.Height <- 500
  156.     form.Show()
  157.     //timer.Start()
  158.  
  159.     Application.Run(form)
  160.  
  161.     let ret = 0
  162.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement