R语言gm玩音乐示例代码Rmarkdown

R语言gm玩音乐示例代码 --Rmarkdown文档

R语言gm音乐包基本语法

r 复制代码
---
title: "音乐数据处理与分析"
author: "cnliutz海豚鲨鱼"
date: "`r Sys.Date()`"
documentclass: article
fontsize: 12pt
output:
  pdf_document:
    latex_engine: xelatex
    fig_caption: yes
    number_sections: true
header-includes:
  - \usepackage{ctex}
  - \setmainfont{SimSun}
  - \setsansfont{SimHei}
  - \setmonofont{FangSong}

# R语言gm音乐包基本语法
# 加载gm包
library(gm)
# 生成乐谱数据
# Create a flute 乐器74 长笛
flute <- Instrument(41, pan = -90)
flute
slur <- Slur(3, 8) #连音线
# Create a tempo
tempo <- Tempo(60, marking = "Adagio (half = 25)")
notehead <- Notehead(1, shape = "diamond", color = "#800080")
tie <- Tie(1) #连音 
line <- Line(
  pitches = c("E5","E5","E5","G5","A5", "G5", "c5","D5","e5","c5","g4","a4","c5","a4","g4","c-5","g#4"),
  durations = c(1, 1, 1,1,0.5,0.5 )
)
#music 
music <- 
  Music() +
  Meter(3, 4) +  # 4/4拍
  line + #声调 -降调
  flute + 
  tempo +
  notehead  +
  slur +       # lianyinfu
  tie          # 音符序列

# 展示乐谱(生成MP3音乐、可调用MuseScore渲染并打开)
export(music,"~/x.mp3","musescore")
export(music,"~/x.mscz","musescore")

#打开网页、musescore 编辑查看乐曲
show(music,musescore = "-r 800 -T 5")

#vignette("gm") 显示帮助信息

example1

{r} 复制代码
# 假设 gm 支持字符串解析(否则需手动转换)
notes <- c("C4", "E4", "G4", "C4","E4","E4","E4","G4","A4", "G4")
# 转换为 MIDI 编号(手动映射)
instrument= Instrument(77)
note_to_midi <- function(note) {
  notes_map <- list(
    C4 = 60, Cs4 = 61, D4 = 62, Ds4 = 63, E4 = 64, F4 = 65,
    Fs4 = 66, G4 = 67, Gs4 = 68, A4 = 69, As4 = 70, B4 = 71
  )
  notes_map[[note]]
}
pitches <- sapply(notes, note_to_midi)
pitches
music<- Music() +
        Meter(3, 4) +   # 4/4拍
        Line(pitches)+
        instrument

show(music)

example2 混合时值,不同乐器

{r} 复制代码
pitches <-c(67, 67, 67, 62, 65, 67, 69, 67)
durations <- c(0.5, 0.5, 1, 2, 0.5, 0.5, 1, 2)   # 八分+八分+四分 / 二分 / ...
tempo = Tempo(120)
velocity = Velocity(100)  #力度
riff <-Music()+ 
  Meter(3,4)+
  Line(pitches,durations) +
  tempo+ 
  velocity

export(riff, "~\\riff.mid")
show(riff)

example3 简单旋律

{r} 复制代码
library(gm)

# 定义时值(全部为四分音符)
instrument = Instrument(41) #小提琴
tempo= Tempo(90)
line <- Line(
  pitches = c(60, 62, 64, 65, 67, 69, 71, 72,73,69, 71, 72,73) , 
  durations = c(1, 1, 1.5, 0.5,1)
)
# 创建音乐线条
melody <- 
  Music() +
  Meter(4, 4) +   # 4/4拍
  line +
  instrument + 
  tempo          # 音符序列

# 播放或导出
#show(melody,musescore = "-r 800 -T 5") 
show(melody) # 如果有音频输出支持
export(melody, "~/scale.mid","musescore")
相关推荐
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁4 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超4 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int4 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
CoderYanger4 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
伞伞悦读4 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json
CCCCCCCCharlie4 天前
Linux进程控制四大核心操作
linux·开发语言
Brilliantwxx4 天前
【STM32】 SPI Flash 驱动开发实战:阻塞模式驱动 W25Q64(含工程代码)
开发语言·stm32·单片机·嵌入式硬件