File: display_curses.sml

package info (click to toggle)
smlsharp 4.2.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 125,348 kB
  • sloc: ansic: 16,737; sh: 4,347; makefile: 2,228; java: 742; haskell: 493; ruby: 305; cpp: 284; pascal: 256; ml: 255; lisp: 141; asm: 97; sql: 74
file content (66 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(**
 * display_curses.sml
 *
 * @copyright (C) 2021 SML# Development Team.
 * @author UENO Katsuhiro
 * @version $Id: display_curses.sml,v 1.3 2007/04/02 09:42:29 katsu Exp $
 *)

structure Display : DISPLAY =
struct

  structure PS = PowerSpectrum(
                   val numSamples = 2048
                   val samplingFreq = 44100.0
                 )
  structure SA = SpectrumAnalyzer(PS)
  structure IN = Input(PS)

  val interval = floor (PS.interval * 1000000.0)  (* usec *)

  fun repeat 0 f = ()
    | repeat n f = (f n; repeat (n - 1) f)

  val buf = Array.array (80, 0.0)

  fun mainLoop input =
      let
        val _ = IN.fill input
        val _ = IN.read input
      in
        SA.toMonoral (IN.buffer, PS.samples);
        PS.calc ();
        SA.powerToHeight PS.spectrum;
        SA.summarize (PS.spectrum, 0, Array.length PS.spectrum, buf);

        Libcurses.clear ();
        Array.appi
          (fn (x, h) =>
              repeat (floor (h / 100.0 * 20.0))
                     (fn y =>
                         (Libcurses.move (20 - y, x);
                          Libcurses.addch #"#")))
          buf;

        Libcurses.refresh ();
        Libc.usleep (interval div 4);
        if IN.finished input then () else mainLoop input
      end

  fun main () =
      let
        val input = IN.openInput ()
      in
        Libcurses.initscr ();
        Libcurses.nocbreak ();
        Libcurses.noecho ();
        Libcurses.nonl ();
        Libcurses.clear ();
        Libcurses.refresh ();
        IN.startInput input;
        mainLoop input;
        IN.closeInput input;
        Libcurses.endwin ()
      end

end