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")
相关推荐
无限的鲜花4 小时前
反射(原创推荐)
java·开发语言
yongche_shi4 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
一路向北he4 小时前
字节钢铁军团--“提供情境,而非控制”
java·开发语言·前端
AI行业学习6 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程6 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
upgrador7 小时前
基础知识:C++ STL构造函数的左闭右开惯例及其实现原理
开发语言·c++
yoothey8 小时前
报废审批流规则引擎设计——责任链模式完整实现
linux·开发语言·bash
geovindu8 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
wuyk5558 小时前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机
凯瑟琳.奥古斯特9 小时前
K次取反最大化数组和解法(力扣1005)
开发语言·c++·算法·leetcode·职场和发展