名前:niszet
R歴:約1年
趣味:R、楽器(お休み中)、ラズパイ
普段は回路設計などをして生きてます
integer
numeric
logical
factor
character
raw
(今回紹介)complex
(機会があれば)1byte = 8bit = 2**8 = 0-255
までを表すことが出来るバイナリファイル(データ)を扱う場合に使用する
complex
と共に大体のRの書籍では紹介されない影の存在as.raw()
で0-255の整数を与えると作れる
raw
はRのmode
の中でも最弱…c("a", as.raw(1)); c(1.0+1.0i, as.raw(1));
## [1] "a" "01"
## [1] 1+1i 1+0i
c(1.0, as.raw(1)); c(TRUE, as.raw(1));
## [1] 1 1
## [1] TRUE TRUE
operator
はほとんど使えない# as.raw(1)+as.raw(1)
## Error in as.raw(1) + as.raw(1) :
## non-numeric argument to binary operator
as.raw(1)<as.raw(2)
## [1] TRUE
raw型に対しては&
や|
はbit単位でのAnd/Orをとる演算子となる。
# 0011_1100(0x3c) & 0110_0110(0x66) = 0010_0100(0x24)
as.raw(60) & as.raw(102)
## [1] 24
# 0011_1100(0x3c) & 0110_0110(0x66) = 0111_1110(0x7e)
as.raw(60) | as.raw(102)
## [1] 7e
ベクトル化も可能
c(as.raw(12),as.raw(255)) & c(as.raw(255),as.raw(1))
## [1] 0c 01
connection
から)値を読む
readBin
readChar
(ascii
の場合)scan
writeBin
writeChar
(ascii
の場合)大体の場合はas.raw()
とas.integer()
で相互に型を変換して使うことになるかと思います 使い方はヘルプを参照ください(一番詳しい資料はヘルプ)
tuneR
パッケージにもtuneR::readMidi
がありますが、欲しいものとちょっと違う…
tuneR::readWave
でOK# rsmf型として一旦ファイルを読む
smf <- read_smf("midi/AveMaria.mid")
# rsmf型から音の高さやなる時間などを抜き出してdata.frameに変換。
nf <- make_note_frame(smf)
# データの中身
head(nf, n=4)
## ch height val start_time end_time nn
## 2 0 53 30 0 48 5
## 3 0 57 30 48 96 9
## 4 0 60 30 96 144 0
## 5 0 65 30 144 192 5
# read_smfの中身
head(read_smf)
##
## 1 function (file)
## 2 {
## 3 con <- file(file, "rb")
## 4 on.exit(close(con))
## 5 file_size <- file.info(file)[["size"]]
## 6 smf_header <- read_header(con)
# その中でreadBinを使用しています
head(rmusicwork:::read_track_data_size)
##
## 1 function (con)
## 2 {
## 3 tds <- readBin(con, "integer", n = 1L, size = 4L, endian = "big")
## 4 tds
## 5 }
# geom_segment で始点と終点になる列を指定。はじめの1/3だけplot
nf %>% ggplot()+geom_segment(aes(
x=start_time, y=height, xend=end_time,yend=height,
color=as.factor(nn), size=1)) + xlim(c(0,10000))+
xlab("time") + ylab("height") + theme(legend.position="none")+
scale_colour_brewer(palette = "Paired")