我想画点东西像这样钢琴,但所有音符都明确地画出并标有其频率. 我第一次使用musixtex
包所以我不知道如何自动化注释。
这是我迄今为止所做的事情。
\documentclass[preview,border={3cm 2mm 3cm 2mm}]{standalone}% standalone works with adjusting the border.
\usepackage{musixtex}
\begin{document}
\begin{music}
\instrumentnumber{1}
\setstaffs{1}{2}
\setclef{1}{60}
%
\startextract
\notes\qu{`CDEFG 'AB}\enotes
\bar
\notes\qu{CDEFG 'AB}\enotes
\bar
\notes\qu{'CDEFG 'AB}\enotes
\bar
\notes|\qu{cdefg'ab}\enotes
\bar
\notes|\qu{'cdefg 'ab}\enotes
\zendextract
\end{music}
\end{document}
答案1
我不确定我是否理解正确,而这正是您想要的,但我们还是来看看:代码利用了这样一个事实:音高不仅可以用字母指定,还可以用数字指定。频率是使用链接的维基百科网站上的公式计算的。(我不知道为什么standalone
必须手动放大边距才能看到整个摘录……)。
% arara: musixtex
\documentclass[preview,border={17cm 2mm 17cm 2mm}]{standalone}
\usepackage[T1]{fontenc}
\usepackage{musixtex}
\usepackage{xcolor}
\usepackage{expl3,xparse}
\ExplSyntaxOn
% define integer variables:
\int_new:N \l__cjorssen_pitch_int
\int_new:N \l__cjorssen_step_int
% define tokenlist variable for the formatted output of the
% frequencies:
\tl_new:N \l__cjorssen_freq_format_tl
\tl_set:Nn \l__cjorssen_freq_format_tl { \tiny\bfseries }
% macro to write notes and frequencies for one octave
% #1: the lowest pitch
% #2: the highest pitch, should not be more than #1+6
% #3: the difference between pitch number and key number
\cs_new_protected:Npn \cjorssen_write_notes:nnn #1#2#3
{
% set the starting pitch:
\int_set:Nn \l__cjorssen_pitch_int { #1 }
% set the step to zero:
\int_zero:N \l__cjorssen_step_int
% loop until we reach the end pitch:
\int_do_while:nn { \l__cjorssen_pitch_int <= #2 }
{
% \cchar{<pitch>}{<something>} is a musixtex macro that
% allows setting <something> at the specified <pitch>
% let's set the frequency below the tone it belongs to:
\cchar
{ \int_eval:n { \l__cjorssen_pitch_int - 5 } }
{
% start a group to keep the format local, just in case
\group_begin:
% the format:
\tl_use:N \l__cjorssen_freq_format_tl
% let's test at which point of the octave we are
% and add the needed integer to \l__cjorssen_pitch_int
% in order to get the right key number with respect to
% musixtex's pitch number:
\int_case:nnn { \l__cjorssen_step_int }
{
{ 0 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + #3 } }
{ 1 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 1 + #3 } }
{ 2 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 2 + #3 } }
{ 3 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 2 + #3 } }
{ 4 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 3 + #3 } }
{ 5 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 4 + #3 } }
{ 6 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 5 + #3 } }
}
{}
\,Hz
\group_end:
}
% write the note:
\qu { \int_use:N \l__cjorssen_pitch_int }
% step the integers for the next round in the loop:
\int_incr:N \l__cjorssen_step_int
\int_incr:N \l__cjorssen_pitch_int
}
}
% calculate the frequency from the key number
% (using 440Hz for the concert pitch):
\cs_new:Npn \cjorssen_calc_freq:n #1
{ \fp_eval:n { round ( 440 * 2^(((#1)-49)/12) , 2 ) } }
% #1: the lowest pitch
% #2: the highest pitch, should not be more than #1+6
% #3: the difference between pitch number and key number
\NewDocumentCommand \writenotes { mO{#1+6}m }
{ \cjorssen_write_notes:nnn { #1 } { #2 } { #3 } }
\ExplSyntaxOff
\begin{document}
\noindent
\begin{music}
\instrumentnumber{1}
\setstaffs{1}{2}
\setclef{1}{60}
\setname{1}{}
\startextract
\NOTes
\writenotes{-11}{15}
\en\bar
\NOTes
\writenotes{-4}{20}
\en\bar
\NOTes
\writenotes{3}{25}
\en\bar
\NOTEs|
\writenotes{-2}{42}
\en\bar
\NOTEs|
\writenotes{5}{47}
\en
\endextract
\end{music}
\end{document}
下面是与article
课程相同的内容:
% arara: musixtex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{musixtex}
\usepackage{xcolor}
\usepackage{expl3,xparse}
\ExplSyntaxOn
% define integer variables:
\int_new:N \l__cjorssen_pitch_int
\int_new:N \l__cjorssen_step_int
% define tokenlist variable for the formatted output of the
% frequencies:
\tl_new:N \l__cjorssen_freq_format_tl
\tl_set:Nn \l__cjorssen_freq_format_tl { \tiny\bfseries }
% macro to write notes and frequencies for one octave
% #1: the lowest pitch
% #2: the highest pitch, should not be more than #1+6
% #3: the difference between pitch number and key number
\cs_new_protected:Npn \cjorssen_write_notes:nnn #1#2#3
{
% set the starting pitch:
\int_set:Nn \l__cjorssen_pitch_int { #1 }
% set the step to zero:
\int_zero:N \l__cjorssen_step_int
% loop until we reach the end pitch:
\int_do_while:nn { \l__cjorssen_pitch_int <= #2 }
{
% \cchar{<pitch>}{<something>} is a musixtex macro that
% allows setting <something> at the specified <pitch>
% let's set the frequency below the tone it belongs to:
\cchar
{ \int_eval:n { \l__cjorssen_pitch_int - 5 } }
{
% start a group to keep the format local, just in case
\group_begin:
% the format:
\tl_use:N \l__cjorssen_freq_format_tl
% let's test at which point of the octave we are
% and add the needed integer to \l__cjorssen_pitch_int
% in order to get the right key number with respect to
% musixtex's pitch number:
\int_case:nnn { \l__cjorssen_step_int }
{
{ 0 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + #3 } }
{ 1 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 1 + #3 } }
{ 2 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 2 + #3 } }
{ 3 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 2 + #3 } }
{ 4 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 3 + #3 } }
{ 5 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 4 + #3 } }
{ 6 } { \cjorssen_calc_freq:n { \l__cjorssen_pitch_int + 5 + #3 } }
}
{}
\,Hz
\group_end:
}
% write the note:
\qu { \int_use:N \l__cjorssen_pitch_int }
% step the integers for the next round in the loop:
\int_incr:N \l__cjorssen_step_int
\int_incr:N \l__cjorssen_pitch_int
}
}
% calculate the frequency from the key number
% (using 440Hz for the concert pitch):
\cs_new:Npn \cjorssen_calc_freq:n #1
{ \fp_eval:n { round ( 440 * 2^(((#1)-49)/12) , 2 ) } }
% #1: the lowest pitch
% #2: the highest pitch, should not be more than #1+6
% #3: the difference between pitch number and key number
\NewDocumentCommand \writenotes { mO{#1+6}m }
{ \cjorssen_write_notes:nnn { #1 } { #2 } { #3 } }
\ExplSyntaxOff
\begin{document}
\noindent
\begin{music}
\instrumentnumber{1}
\setstaffs{1}{2}
\setclef{1}{60}
\setname{1}{}
\startpiece
\NOTes
\writenotes{-11}{15}
\en\alaligne
\NOTes
\writenotes{-4}{20}
\en\alaligne
\NOTes
\writenotes{3}{25}
\en\alaligne
\NOTEs|
\writenotes{-2}{42}
\en\alaligne
\NOTEs|
\writenotes{5}{47}
\en
\endpiece
\end{music}
\end{document}