Recognizes if input is ogg or not.
[xiph.git] / py-ogg2 / examples / writ_encoder.py
blobc39f15ae3e466dee4f51f1e8da8697e4dd81b489
1 '''
2 function: Ogg Writ 1.2 reference encoder
3 last mod: $Id: writ_encoder.py,v 1.2 2004/02/24 06:31:49 arc Exp $
5 This is an example for how py-ogg2 can be used to rapidly design a new
6 Ogg codec or test an existing codec's specifications for accuracy.
8 '''
10 import ogg2
11 import struct
12 import random
14 class BitPacker :
15 def __init__(self) :
16 self.pb = ogg2.OggPackBuff()
18 def uniwrite(self,ustring) :
19 total = 0
20 output=[]
21 for pos in range(len(ustring)) :
22 value = ord(ustring[pos])
23 if value < 256 :
24 size = 8
25 elif value < 65536 :
26 size = 16
27 elif value < 16777216 :
28 size = 24
29 else :
30 size = 32
31 total = total + (size/8)
32 output.append((value,size))
33 if total>255 :
34 raise('ValueError: Unicode must be no greater than 255 bytes long')
35 self.pb.write(total,8)
36 for char in output :
37 self.pb.write(char[0],char[1])
39 def write(self,value,size) :
40 if size>32 :
41 hi=value/4294967296
42 lo=value-(hi*4294967296)
43 self.pb.write(lo,32)
44 self.pb.write(hi,size-32)
45 else :
46 self.pb.write(value,size)
48 def export(self) :
49 return self.pb.packetout()
51 def reset(self) :
52 self.pb = ogg2.OggPackBuff()
53 # return self.pb.reset()
55 class OggStream :
56 def __init__(self) :
57 self.os = ogg2.OggStreamState(random.randrange(2**24))
58 self.os.setmode(ogg2.Ogg_Discont)
59 self.oy = ogg2.OggSyncState()
60 self.fd = open('test.writ.ogg','w')
61 self.pn = 0
63 def packetin(self, packet, end_granule=0) :
64 packet.packetno = self.pn
65 packet.end_granule = end_granule
66 if self.pn == 0 :
67 packet.bos = 1
68 else :
69 packetno = self.packet.packetno
70 self.os.packetin(self.packet)
71 if self.pn != 2 :
72 self.flush()
73 self.packet = packet
74 self.pn = self.pn + 1
76 def flush(self) :
77 page = self.os.flush()
78 if page != None :
79 self.oy.pagein(page)
80 output = '.'
81 while len(output)!= 0 :
82 output = self.oy.read(4096)
83 self.fd.write(output)
85 def close(self) :
86 self.packet.eos = 1
87 self.os.packetin(self.packet)
88 self.flush()
89 self.fd.close()
92 def ilog(num) :
93 for x in range(17) :
94 if 2**x >= num+1 :
95 return x
96 return 0
99 gnum = 1
100 gden = 1
102 langs=(('en','English'),('es','Spanish'))
104 # winds: location_x, location_y, width, height, alignment_x, alignment_y
105 sclx = 4000
106 scly = 270
107 bitx = ilog(sclx)
108 bity = ilog(scly)
109 totl = (bitx*2)+(bity*2)+4
110 bitp = ((((totl-1)/8)+1)*8)-totl
111 winds=((1,2,3,1,3,3),(5,6,7,1,3,3))
113 # texts: start, duration, lang(s), wind
114 texts=((05,10, ( u'Hello World!' ,
115 u'Hola, Mundo!' ), 0),
116 (12,15, ( u'It\'s a beautiful day to be born.' ,
117 u'Es un d\N{LATIN SMALL LETTER I WITH ACUTE}'+\
118 u'a hermoso para que se llevar'+\
119 u'\N{LATIN SMALL LETTER A WITH ACUTE}.' ), 1) )
121 bp = BitPacker()
122 os = OggStream()
124 # Start with Header 0
125 bp.write(0,8) # header packet 0
126 bp.write(1953067639,32) # "writ"
127 bp.write(1,8) # version = 1
128 bp.write(2,8) # subversion = 2
129 bp.write(gnum,32) # granulerate_numerator
130 bp.write(gden,32) # granulerate_denominator
131 os.packetin(bp.export())
132 bp.reset()
134 # Start with Header 1
135 bp.write(1,8) # header packet 1
136 bp.write(1953067639,32) # "writ"
137 bp.write(len(langs)-1,8) # num_languages
138 for lang in langs:
139 bp.uniwrite(lang[0]) # lang_name length, string
140 bp.uniwrite(lang[1]) # lang_desc length, string
141 os.packetin(bp.export())
142 bp.reset()
144 # Start with Header 1
145 bp.write(2,8) # header packet 2
146 bp.write(1953067639,32) # "writ"
147 bp.write(sclx,16) # location_scale_x
148 bp.write(scly,16) # location_scale_y
149 bp.write(len(winds),8) # num_windows
150 for win in winds:
151 bp.write(win[0],bitx) # location_x
152 bp.write(win[1],bity) # location_x
153 bp.write(win[2],bitx) # location_width
154 bp.write(win[3],bity) # location_height
155 bp.write(win[4],2) # alignment_x
156 bp.write(win[5],2) # alignment_y
157 os.packetin(bp.export())
158 bp.reset()
160 for text in texts: # - Each Phrase -
161 bp.write(255,8) # data packet
162 bp.write(text[0],64) # granule_start
163 bp.write(text[1],32) # granule_duration
164 for lang in range(len(langs)):
165 bp.uniwrite(text[2][lang]) # text length, string
166 if len(winds) > 1 : # only if we need to specify
167 bp.write(text[3],8) # window_id
168 os.packetin(bp.export(), text[0])
169 bp.reset()
171 os.close()