GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: the flash recorder for chirrp, written with haXe
Homepage: http://chirrp.net/
Clone URL: git://github.com/why/chirrup.git
chirrup / Base.hx
100644 186 lines (156 sloc) 4.784 kb
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import flash.events.NetStatusEvent;
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
import flash.system.Security;
import flash.system.SecurityPanel;
import flash.text.TextField;
import flash.text.TextFormat;
 
class Base {
 
  static var rtmp = "rtmp://chirrp.net";
  static var server = "http://chirrp.net";
  static var seconds : Float = 8;
 
  static var nc : NetConnection;
  static var ns : NetStream;
  static var mic : Microphone;
 
  static var httpExp : EReg = ~/^http:\/\//;
 
  static var DEGTORAD:Float = Math.PI/180;
  static var a:Float = Math.tan( 22.5 * DEGTORAD);
 
  static var UID_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  
  public static function random(?size : Int) : String
  {
    if(size == null) size = 32;
    var nchars = UID_CHARS.length;
    var uid = "";
    for (i in 0 ... size){
      uid += UID_CHARS.charAt( Std.int(Math.random() * nchars) );
    }
    return uid;
  }
 
  static function privacyPane() {
    Security.showSettings(SecurityPanel.PRIVACY);
  }
 
  static function micPane() {
    Security.showSettings(SecurityPanel.MICROPHONE);
  }
 
  static function doTrace( v : Dynamic, ?pos : haxe.PosInfos ) {
    trace(pos.fileName+"("+pos.lineNumber+") : "+Std.string(v));
  }
 
  static function doClick( onClick, e ) {
    try {
      onClick();
    } catch( e : Dynamic ) {
      doTrace(e);
    }
  }
 
  static function addBitmap( bmp ) {
    flash.Lib.current.addChild(new flash.display.Bitmap(bmp, flash.display.PixelSnapping.AUTO, true));
  }
 
  static function movieBitmap( bmp ) {
    var mc = new flash.display.MovieClip();
    mc.addChild(new flash.display.Bitmap(bmp, flash.display.PixelSnapping.AUTO, true));
    return mc;
  }
 
  static function addBitmapButton( bmpOff, bmpOn, bmpHit, onClick )
  {
    var sb = new flash.display.SimpleButton();
    sb.upState = movieBitmap(bmpOff);
    sb.overState = movieBitmap(bmpOff);
    sb.downState = movieBitmap(bmpOn);
    sb.hitTestState = bmpHit;
    sb.useHandCursor = true;
    sb.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, onClick);
    flash.Lib.current.addChild(sb);
    return sb;
  }
 
  static function drawCircle( mc:flash.display.MovieClip, x:Float, y:Float, r:Float )
  {
    mc.graphics.moveTo( x + r, y );
 
    for (i in 0...8) {
      var endX = x + r * Math.cos( (i + 1) * 45 * DEGTORAD);
      var endY = y + r * Math.sin( (i + 1) * 45 * DEGTORAD);
      var controlX = endX + r * a * Math.cos( ((i + 1) * 45 - 90) * DEGTORAD);
      var controlY = endY + r * a * Math.sin( ((i + 1) * 45 - 90) * DEGTORAD);
      mc.graphics.curveTo( controlX, controlY, endX, endY);
    }
  }
 
  static function addLink( text, onClick ) {
    var f = new TextFormat();
    f.font = "Verdana";
    f.size = 12;
    f.color = 0x4488AA;
    f.underline = true;
 
    var f2 = new TextFormat();
    f2.font = "Verdana";
    f2.size = 12;
    f2.color = 0xAA6633;
    f2.underline = true;
 
    var t = new TextField();
    t.text = text;
    t.width = t.textWidth + 36;
    t.height = 18;
    t.selectable = false;
    t.setTextFormat(f);
    t.x = 2;
 
    var t2 = new TextField();
    t2.text = text;
    t2.width = t.textWidth + 36;
    t2.height = 18;
    t2.selectable = false;
    t2.setTextFormat(f2);
    t2.x = 2;
 
    var a = new flash.display.MovieClip();
    a.addChild(t);
 
    var b = new flash.display.MovieClip();
    b.graphics.beginFill(0);
    b.graphics.drawRect(0,0,t.width,18);
 
    var c = new flash.display.MovieClip();
    c.addChild(t2);
 
    var sb = new flash.display.SimpleButton();
    sb.upState = a;
    sb.overState = c;
    sb.downState = c;
    sb.hitTestState = b;
    sb.useHandCursor = true;
    sb.addEventListener(flash.events.MouseEvent.CLICK, onClick);
    return sb;
  }
 
  static function startConnect(host, func) {
    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, func);
    nc.connect(host);
  }
 
  static function play(filename) {
    var host : String = rtmp;
    if (httpExp.match(filename))
      host = null;
    Base.startConnect(host, function(e) {
      if (e.info.code == "NetConnection.Connect.Success") {
        ns = new NetStream(nc);
        ns.play(filename);
      }
    });
  }
 
  static function record(filename, func) {
    NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;
    mic = Microphone.getMicrophone(-1);
    mic.rate = 44;
    Base.startConnect(rtmp, function(e) {
      if (e.info.code == "NetConnection.Connect.Success") {
        ns = new NetStream(nc);
        ns.addEventListener(NetStatusEvent.NET_STATUS, function(e) {});
        ns.attachAudio(mic);
        ns.publish(filename);
        func(ns);
      }
    });
  }
 
  static function stop() {
    ns.close();
    nc.close();
  }
 
  static function elapsedTime() {
    return ns.time;
  }
}