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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | package wavspliter {
import java.io._ import java.nio.{ ByteBuffer, ByteOrder } import scala.math._
object Main { import WaveSpliter._
def main( args: Array[String] ) {
val silent = 100 // 無音の定義 val nsilent = 10000 // 無音の持続時間 val minsize = 200000 // 分割ファイルの最小サイズ
// コマンドライン引数のチェック val (idir, ifile, odir) = args match { // 引数が2つある場合。入力ファイルと出力ディレクトリ case Array( x, y ) => val f = new File(x) (f.getParent, convRegex(f.getName), y) // 引数が1つの場合。 case Array( x ) => val f = new File(x) (f.getParent, convRegex(f.getName), f.getParent) case _ => throw new Exception("invalid number of arguments") }
val matcher: File => Boolean = _.getAbsolutePath.matches(ifile) val splitter = split(new File(odir), silent, nsilent, minsize) _
((new File( idir )).listFiles filter matcher) foreach splitter
}
}
object WaveSpliter {
object Info {
val ISRC = "ISRC" // メディア元 val ICRD = "ICRD" // 作成日 val IPRD = "IPRD" // アルバム名 val INAM = "INAM" // 曲名 val IGNR = "IGNR" // ジャンル val ICMT = "ICMT" // コメント val IART = "IART" // アーティスト val ICMS = "ICMS" // 著作権代理人 val ICOP = "ICOP" // 著作権情報 val IENG = "IENG" // 録音エンジニア val ISFT = "ISFT" // ソフトウェア val IKEY = "IKEY" // キーワード val ITCH = "ITEC" // エンコード技術者 val ISBJ = "ISBJ" // タイトル val ITRK = "ITRK" // トラック番号 val ITOC = "ITOC" // 目次
}
// fmt フォーマット case class AudioHeader( val pcmType : Short, val numOfChannels : Short, val samplingRate : Int, val bytesInSec : Int, val blockSize : Short, val sampleSizeInBits : Short, val extSize : Short )
// Wavファイル case class WaveFile( info : Map[String, String], // INFO audioHeader : AudioHeader, // fmt data : Array[Byte], // sample data eachBlock : (List[Short] => Unit) => Unit // 各ブロックを読む関数 )
// バイト列に変換する型クラス trait Bytable { def toBytes: Array[Byte] }
// IntのBytable class BytableInt( x: Int ) extends Bytable { def toBytes: Array[Byte] = ByteBuffer.allocate( 4 ).order( ByteOrder.LITTLE_ENDIAN ).putInt( x ).array() }
// ShortのBytable class BytableShort( x: Short ) extends Bytable { def toBytes: Array[Byte] = ByteBuffer.allocate( 2 ).order( ByteOrder.LITTLE_ENDIAN ).putShort( x ).array() }
// StringのBytable class BytableString( x: String ) extends Bytable { def toBytes: Array[Byte] = x.getBytes }
// INFOのBytable class BytableInfo( x: Map[String, String] ) extends Bytable { def toBytes: Array[Byte] = { val out = new ByteArrayOutputStream x.foreach { case (k, v) => val (size, desc) = byteDesc( v ) write( out, k ) write( out, size ) write( out, desc ) } out.toByteArray } private def byteDesc( x: String ): (Int, Array[Byte]) = { val bs = x.getBytes val size = bs.size val mod = size % 4 val ary = new Array[Byte]( mod ) (size + mod, bs ++ ary) } }
// fmtのBytable class BytableAudioHeader( ah: AudioHeader ) extends Bytable { def toBytes: Array[Byte] = { val out = new ByteArrayOutputStream write( out, ah.pcmType ) write( out, ah.numOfChannels ) write( out, ah.samplingRate ) write( out, ah.bytesInSec ) write( out, ah.blockSize ) write( out, ah.sampleSizeInBits ) write( out, ah.extSize ) out.toByteArray } }
// 暗黙の型変換 implicit def toBytable( x: Int ): Bytable = new BytableInt( x ) implicit def toBytable( x: Short ): Bytable = new BytableShort( x ) implicit def toBytable( x: String ): Bytable = new BytableString( x ) implicit def toBytable( x: Map[String, String] ): Bytable = new BytableInfo( x ) implicit def toBytable( x: AudioHeader ): Bytable = new BytableAudioHeader( x )
// Emptyだったら例外を投げる def get_: A = a match { case Some( x ) => x case _ => throw new Exception( "option is empty!" ) }
def write( out: OutputStream, x: Array[Byte] ) = out.write( x )
// Bytable型をOutputStreamに書き込む def write( out: OutputStream, x: Bytable ) = out.write( x.toBytes )
// wavファイルを出力する def write( file: File, info: Map[String, String], audioHeader: AudioHeader, data: Array[Byte] ): Unit = { val out = new BufferedOutputStream( new FileOutputStream( file ) ) val infoBytes = info.toBytes val ahBytes = audioHeader.toBytes
val infoSize = infoBytes.size val listSize = 4 + infoSize // 'INFO' + infoSize val fmtSize = ahBytes.size // ahSize val dataSize = data.size // dataSize val fSize = 4 + listSize + fmtSize + dataSize // 'WAVE' + listSize + fmtSize + dataSize write( out, "RIFF" ) write( out, fSize ) write( out, "WAVE" ) write( out, "LIST" ) write( out, listSize ) write( out, "INFO" ) write( out, infoBytes ) write( out, "fmt " ) write( out, fmtSize ) write( out, ahBytes ) write( out, "data" ) write( out, dataSize ) write( out, data ) out.flush out.close }
def readBytes( in: InputStream, size: Int ) = { val bytes = new Array[Byte]( size ) if( in.read( bytes ) < size ) None else Some( bytes ) }
// InputStreamから文字列を読み取る def readString( in: InputStream, size: Int ) = readBytes( in, size ).map( b => new String( b ) )
// InputStreamからIntを読み取る def readInt( in: InputStream ) = readBytes( in, 4 ).map( b => ByteBuffer.wrap( b ).order( ByteOrder.LITTLE_ENDIAN ).getInt )
// InputStreamからShortを読み取る def readShort( in: InputStream ) = readBytes( in, 2 ).map( b => ByteBuffer.wrap( b ).order( ByteOrder.LITTLE_ENDIAN ).getShort )
// 一部をInputStreamに変換する def readAsInputStream( in: InputStream, size: Int ) = readBytes( in, size ).map( toInputStream )
// バイト列をInputStreamに変換する def toInputStream( bytes: Array[Byte] ) = new ByteArrayInputStream( bytes )
// チャンクの名前とサイズを読み取る def readChunkHeader( in: InputStream ) = for { name <- readString( in, 4 ) size <- readInt( in ) } yield ( name, size )
// 全てのチャンクを読み取って、各チャンクをfuncに渡す def readChunk( in: InputStream )( func: PartialFunction[(String, Array[Byte]), Unit] ): Unit = readChunkHeader( in ) match { case None => () case Some( (tag, size) ) => readBytes( in, size ).foreach { bs => if( func.isDefinedAt( (tag, bs) ) ) func( (tag, bs) ) readChunk( in )( func ) } }
// INFOのLISTチャンクを読み取る def readListChunk( in: InputStream ): Map[String, String] = readString( in, 4 ) match { case Some( "INFO" ) =>
def readInfoItem: Option[(String, String)] = for { tag <- readString( in, 4 ) size <- readInt( in ) desc <- readString( in, size ) } yield { readBytes( in, size % 2 ) ( tag, desc ) }
def readInfo( x: Option[(String, String)] ): List[(String, String)] = x match { case Some( y ) => y :: readInfo( readInfoItem ) case _ => Nil }
readInfo( readInfoItem ).toMap case _ => Map[String, String]() }
// fmtチャンクを読み取る def readFmtChunk( in: InputStream ) = AudioHeader( get_!( readShort( in ) ), get_!( readShort( in ) ), get_!( readInt( in ) ), get_!( readInt( in ) ), // byte/sec get_!( readShort( in ) ), // get_!( readShort( in ) ), // bit/sample get_!( readShort( in ) ) )
// LIST, fmt, dataチャンクを読み取る def readChunks( in: InputStream ):(Map[String, String], AudioHeader, Array[Byte]) = { var info = Map[String, String]() var audioHeader: AudioHeader = null var data: Array[Byte] = null
readChunk( in ) { case ("data", bs) => data = bs case ("LIST", bs) => info = readListChunk( toInputStream( bs ) ) case ("fmt ", bs) => audioHeader = readFmtChunk( toInputStream( bs ) ) }
if( data == null ) throw new Exception("data chunk is null") if( audioHeader == null ) throw new Exception("fmt chunk is null") ( info, audioHeader, data ) }
// ブロックを読み取る def readBlock( data: InputStream, nChan: Short ): List[Short] = {
def func( data: InputStream, nChan: Short, cnt: Int ): List[Short] = if( cnt == nChan ) { readShort( data ).toList } else if( cnt < nChan ) readShort( data ) match { case None => Nil case Some( x ) => func( data, nChan, cnt + 1 ) match { case Nil => Nil case xs => x :: xs } } else Nil
func( data, nChan, 1 ) }
def convRegex( s: String ) = s.replaceAll("""\.""", """\\.""").replaceAll("""\*""", """.*""")
// WAVファイルを開く def openWave( file: File ): WaveFile = {
val in = new BufferedInputStream( new FileInputStream( file ) ) val riff = get_!(readString( in, 4 )) val fileSize = get_!(readInt( in )) val wave = get_!(readString( in, 4 ))
val (info, audioHeader, data) = readChunks( in )
in.close
WaveFile(info, audioHeader, data, { func => val in = toInputStream( data ) var block = readBlock( in, audioHeader.numOfChannels ) while( block != Nil ) { func( block ) block = readBlock( in, audioHeader.numOfChannels ) } } ) }
// wavファイルを無音区間で分割する def split( odir: File, silent: Int, nsilent: Int, minsize: Int )( input: File ) = {
val wf = openWave( input )
// wavファイルを解析する為のマップ //val map = new scala.collection.mutable.HashMap[Int, Int]
var output = new ByteArrayOutputStream var cnt1 = 0 var cnt2 = 0 wf.eachBlock { xs => if( abs(xs.head) < silent ) cnt1 += 1 else {
// 無音の連続回数を記録する //if( cnt1 > 0 ) { // map.get( cnt1 ) match { // case Some( n ) => map.update( cnt1, n + 1 ) // case None => map.update( cnt1, 1 ) // } //}
if( cnt1 > nsilent && output.size > minsize ) { cnt2 += 1 val bytes = output.toByteArray val size = output.size val (b1, b2 ) = bytes.splitAt( size - nsilent/2 ) WaveSpliter.write( outputFile( input, odir, cnt2 ), wf.info + (Info.ITRK -> cnt2.toString, Info.INAM -> "English Phrase", Info.IGNR -> "English"), wf.audioHeader, b1 ) output = new ByteArrayOutputStream output.write( b2 ) } cnt1 = 0 } xs.foreach { x => output.write( x.toBytes ) } }
WaveSpliter.write( outputFile( input, odir, cnt2 + 1 ), wf.info + (Info.ITRK -> (cnt2 + 1).toString, Info.INAM -> "English Phrase", Info.IGNR -> "English"), wf.audioHeader, output.toByteArray )
// wavファイル内の無音の連続区間を長い順に表示する //var n = 0; //map.toList.sortWith { case ((x, _), (y, _)) => x > y }.foreach { case (k, v) => // n += v // println( k + ": " + v + ", " + n ) //}
}
// 出力ファイルを作成する def outputFile( input: File, odir: File, idx: Int ) = { val ofile = input.getName.replaceAll("""\.([^\.]+)$""", "_%d.out.$1".format( idx ) ) val opath = "%s\\%s".format(odir.getPath, ofile) println("%s" format opath) new File( opath ) }
}
} |