[go: up one dir, main page]

File: aiff.tcl

package info (click to toggle)
cecilia 2.0.5-2.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,440 kB
  • ctags: 833
  • sloc: tcl: 9,786; sh: 1,085; makefile: 69; csh: 13
file content (148 lines) | stat: -rw-r--r-- 4,325 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
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
#
# 	Analyse d'entete de fichiers AIFF
# 	(c) 1997 Alexandre Burton
# 	v1.80a (10/08/97)
#

proc GetByte { fid } {
    set byte [read $fid 1]
    if {[string length $byte] == 1} {
    	if { $byte != "" } {
			scan $byte "%c" val
			return $val
		} else { return 0 }
	} else { if [eof $fid] {return -1} else {return 0} }
}

proc ieee80toSR {ieee80} {
	switch -- [format %.0f [expr  $ieee80 / 1.0e+18]] {
		302503	{puts ***trapped\ 48000}
		302502	{puts ***trapped\ 44100}
		302434	{return 32000}
		302433	{puts ***trapped\ 32000}
		302428	{puts ***trapped\ 22050}
		302471	{puts ***trapped\ 16000}
		302465	{puts ***trapped\ 11025}
		302453	{puts ***trapped\   8000}
	}

    if {$ieee80 == 3.02503e+23} {return 48000}
    if {$ieee80 == 3.0250212236716217e+23} {return 44100}  
	if {$ieee80 == 3.025018341e+23} {return 44100}
    if {$ieee80 == 3.02502e+23} {return 44100}    
    if {$ieee80 == 3.025021223e+23} {return 44100}    
	if {$ieee80 == 3.02501834137e+23} {return 44100} 
    if {$ieee80 == 3.02434e+23} {return 32000}    
    if {$ieee80 == 3.0243393674290388e+23} {return 32000}
    if {$ieee80 == 3.02428e+23} {return 22050}    
    if {$ieee80 == 3.02471e+23} {return 16000}    
    if {$ieee80 == 3.02465e+23} {return 11025}    
    if {$ieee80 == 3.02453e+23} {return 8000} 
    puts "erreur, ieee80 ($ieee80) inconnu"
    return 0
}

proc joinBytes {file from to mode} {
    if { $from < $to } { set step 1 } { set step -1 }
    switch $mode {
		text {
		    set r ""
		    for { set i $from } { $i <= $to } { incr i $step } {
			append r [format %c [GetByte $file]] } } 	
		value {
		    set r 0
		    for { set i $from } { $i >= $to } { incr i $step } {
			set m [expr pow(256,$i)]
			set r [expr $r+([GetByte $file]*$m)] } } 	
		default {
		    puts "error: unknown joinBytes mode"
		    return 0 } }
    return $r 
}


proc aiffread {path} {

    set f [open $path r]
    set form [joinBytes $f 0 3 text]

	if {$form == "RIFF"} {wavread $f; return}

    if {$form != "FORM"} {
		puts "erreur: ne semble pas etre un fichier AIFF"
		return 0 }

    set size   [expr int([joinBytes $f 3 0 value])]
    set format [string tolower [joinBytes $f 0 3 text]]
	set chunk  [joinBytes $f 0 3 text]
    set csize  [joinBytes $f 3 0 value]
    set chans  [expr int([joinBytes $f 1 0 value])]
    set frames [expr int([joinBytes $f 3 0 value])]
    set bits   [expr int([joinBytes $f 1 0 value])]
    set ieee80 [joinBytes $f 9 0 value]

puts "$size $format $chunk $csize $chans $frames $bits"
    if ![set rate [ieee80toSR $ieee80]] {
	return 0 }

    set duree [format %.4f [expr 1.0 * $frames / $rate]]
#    set next [joinBytes $f 0 3 text]

#    if {$next != "MARK"} {
#	puts "erreur: probleme de chunks, sans doute AIFC"
#	return 0 }
    
    close $f
    return [concat $duree $rate $format $chans $bits]
}


proc wavread {path} {
set f $path
puts "looks like a RIFF"
    set size   [joinBytes $f 3 0 value]
    set format [joinBytes $f 3 0 text]
    set fmt  [joinBytes $f 0 3 text]
    set fmtsize  [expr int([joinBytes $f 0 3 value])]
    set fmtag  [expr int([joinBytes $f 0 1 value])]
    set chans [expr int([joinBytes $f 0 1 value])]
    set sr [expr int([joinBytes $f 0 3 value])]
    set frames [joinBytes $f 0 3 value]
    set byts   [expr int([joinBytes $f 0 3 value])]
    set bits   [expr int([joinBytes $f 0 1 value])]
    set bchan   [expr int([joinBytes $f 0 1 value])]
    set dat   [expr int([joinBytes $f 0 1 value])]
    set len   [expr int([joinBytes $f 0 3 value])]
#    set ieee80 [joinBytes $f 9 0 value]


puts "length 1: $size \n
format: $format \n
fm: $fmt \n
fmlength: $fmtsize \n
tag: $fmtag \n
chan: $chans \n
sampling rtae: $sr \n
\n
$byts $bits $bchan\n \
	$dat $len"
return
}

#    4 bytes  'RIFF'
#    4 bytes  <length>
#    4 bytes  'WAVE'
#    4 bytes  'fmt '
#    4 bytes  <length>       ; 0x10 - length of 'fmt ' block
#    2 bytes  01             ; format tag
#    2 bytes  01             ; channels (1=mono, 2=stereo)
#    4 bytes  xxxx           ; samples per second
#    4 bytes  xxxx           ; bytes per second
#    2 bytes  01             ; bytes per sample
#    2 bytes  08             ; bits per channel
#    4 bytes  'data'
#    4 bytes  <length>
#      bytes  <sample data>

## pour l'utilisation en command-line UNIX
#if [llength $argv] {puts [aiffread $argv]}