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")
相关推荐
CoderCodingNo3 分钟前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法
独好紫罗兰7 分钟前
对python的再认识-基于数据结构进行-a005-元组-CRUD
开发语言·数据结构·python
Faker66363aaa15 分钟前
药品包装识别与分类系统:基于Faster R-CNN R50 FPN的Groie数据集训练_1
分类·r语言·cnn
chilavert31819 分钟前
技术演进中的开发沉思-356:重排序(中)
java·开发语言
devmoon22 分钟前
为 Pallet 搭建最小化 Mock Runtime 并编写单元测试环境
开发语言·单元测试·区块链·智能合约·polkadot
Coder_Boy_36 分钟前
Java开发者破局指南:跳出内卷,借AI赋能,搭建系统化知识体系
java·开发语言·人工智能·spring boot·后端·spring
Mr_Xuhhh42 分钟前
介绍一下ref
开发语言·c++·算法
nbsaas-boot1 小时前
软件开发最核心的理念:接口化与组件化
开发语言
lsx2024061 小时前
Java 对象概述
开发语言
Mr_Xuhhh1 小时前
C++11实现线程池
开发语言·c++·算法