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")
相关推荐
2601_962071573 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
淡海水5 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
CS_Zero5 小时前
C语言sizeof是函数吗?
c语言·开发语言
-今昭-6 小时前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
黑马程序员毕设8 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
statistican_ABin8 小时前
全球教育水平提升预测分析报告——基于R语言随机森林回归模型的全球40国1960-2020年教育水平预测研究
随机森林·回归·r语言
_Twink1e8 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen
问天_观心9 小时前
大模型微调学习(一)
开发语言·人工智能·学习·语言模型·github
阿里嘎多学长9 小时前
2026-09-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
源代码•宸10 小时前
前置准备:定时微服务背景和现状
开发语言·经验分享·后端·微服务·云原生·架构·golang