[abcjs]. For a complete description of the syntax, see the Plugin Site.
Version: 6.1.3
Author: Paul Rosen
Author URI: http://paulrosen.net
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/*
Copyright (C) 2015-2022 Paul Rosen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//
//-- Allow upload of .abc files in "Add Media"
//
add_filter(
'upload_mimes',
function( $types ) {
return array_merge( $types, array( 'abc' => 'text/plain' ) );
}
);
//
//-- Add the javascript and css if there is a shortcode on the page.
//
function abcjs_conditionally_load_resources( $posts ) {
if ( empty( $posts ) ) {
return $posts;
}
$has_abcjs = false;
foreach ( $posts as $post ) {
if ( stripos( $post->post_content, '[abcjs' ) !== false ) {
$has_abcjs = true;
break;
}
}
if ( $has_abcjs ) {
wp_enqueue_script( 'abcjs-plugin', plugins_url( '/abcjs-basic-min.js', __FILE__ ));
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'style1', $plugin_url . 'abcjs-audio.css' );
}
return $posts;
}
add_filter( 'the_posts', 'abcjs_conditionally_load_resources' );
// This turns the shortcode parameter back into the originally pasted string.
function process_abc( $content ) {
$content2 = preg_replace("&
\r\n&", "\x01", $content);
$content2 = preg_replace("&
\n&", "\x01", $content2);
$content2 = preg_replace("&
\r\n&", "\x01", $content2);
$content2 = preg_replace("&
\n&", "\x01", $content2);
$content2 = preg_replace("&\r\n&", "\x01", $content2);
$content2 = preg_replace("&\n&", "\x01", $content2);
$content2 = preg_replace("-\"-", "\\\"", $content2);
$content2 = preg_replace("-”-", "\\\"", $content2);
$content2 = preg_replace("-„-", "\\\"", $content2);
$content2 = preg_replace("-’-", "'", $content2);
$content2 = preg_replace("-″-", "\\\"", $content2);
$content2 = preg_replace("-“-", "\\\"", $content2);
$content2 = preg_replace("-'-", "\\\'", $content2);
$content2 = preg_replace("&
&", "\x01", $content2);
$content2 = preg_replace("&
&", "\x01", $content2);
return $content2;
}
// If a URL was passed in, then read the string from that, otherwise read the string from the contents.
function get_abc_string( $file, $content) {
if ($file) {
$content2 = file_get_contents( $file );
$content2 = preg_replace("&\r\n&", "\x01", $content2);
$content2 = preg_replace("&\n&", "\x01", $content2);
$content2 = preg_replace("-'-", "\\\'", $content2);
$content2 = preg_replace("-\"-", "\\\"", $content2);
} else
$content2 = process_abc($content);
return $content2;
}
function construct_divs($number_of_tunes, $type, $class) {
$output = "";
$ids = "";
for ($i = 0; $i < $number_of_tunes; $i = $i + 1) {
$id = 'abc-' . $type . '-' . uniqid();
$output = $output . '
' . "\n";
$ids = $ids . "'" . $id . "',";
}
$output = $output . "
";
return array( 'output' => $output, 'ids' => $ids );
}
function construct_divs2($number_of_tunes, $type, $class, $type2, $class2) {
$output = "";
$ids = "";
$ids2 = "";
for ($i = 0; $i < $number_of_tunes; $i = $i + 1) {
$id = 'abcjs-' . $type . '-' . uniqid();
$output = $output . '
' . "\n";
$ids = $ids . "'" . $id . "',";
$id = 'abcjs-' . $type2 . '-' . uniqid();
$output = $output . '
' . "\n";
$ids2 = $ids2 . "'" . $id . "',";
}
$output = $output . "
";
return array( 'output' => $output, 'ids' => $ids, 'ids2' => $ids2 );
}
//
//-- Interpret the [abcjs] shortcode
//
function abcjs_create_music( $atts, $content ) {
$a = shortcode_atts( array(
'class' => 'abc-paper',
'params' => '{}',
'options' => '{}',
'parser' => '{}',
'engraver' => '{}',
'render' => '{}',
'file' => '',
'number_of_tunes' => '1'
), $atts );
if ($a['options'] == '{}')
$a['options'] = $a['params'];
if ($a['options'] == '{}')
$a['options'] = $a['parser'];
$options = $a['options'];
$options = preg_replace("-[-", "[", $options);
$options = preg_replace("-[-", "[", $options);
$options = preg_replace("-]-", "]", $options);
$options = preg_replace("-]-", "]", $options);
$content2 = get_abc_string($a['file'], $content);
$ret = construct_divs($a['number_of_tunes'], 'paper', $a['class']);
$output = $ret['output'];
$ids = $ret['ids'];
$output = $output . openJsSection() .
'var abc = "' . $content2 . '".replace(/\x01/g,"\n");' . "\n" .
'ABCJS.renderAbc([' . $ids . '], ' . "\n" .' abc, ' . "\n" . $options . ', ' . $a['engraver'] . ', ' . $a['render'] . ');' . "\n" .
closeJsSection();
return $output;
}
add_shortcode( 'abcjs', 'abcjs_create_music' );
//
//-- Interpret the [abcjs-midi] shortcode
// This creates only the audio control but no visual music.
//
function abcjs_create_midi( $atts, $content ) {
$a = shortcode_atts( array(
'class' => 'abc-midi',
'params' => '{}',
'options' => '{}',
'parser' => '{}',
'midi' => '{}',
'file' => '',
'number_of_tunes' => '1'
), $atts );
if ($a['parser'] == '{}')
$a['parser'] = $a['params'];
if ($a['parser'] == '{}')
$a['parser'] = $a['options'];
$content2 = get_abc_string($a['file'], $content);
$ret = construct_divs($a['number_of_tunes'], 'midi', $a['class']);
$output = $ret['output'];
$ids = $ret['ids'];
$output = $output . openJsSection() .
'var visualObjs = ABCJS.renderAbc("*", ' . "\n" .
'"' . $content2 . '".replace(/\x01/g,"\n"), ' . "\n" .
$a['parser'] .
', ' .
$a['midi'] .
');' . "\n" .
getSynthInit($ids, $a['midi'], 'null') . "\n" .
closeJsSection();
return $output;
}
add_shortcode( 'abcjs-midi', 'abcjs_create_midi' );
//
//-- Interpret the [abcjs-audio] shortcode
// This creates both the music and the audio control
//
function abcjs_create_audio( $atts, $content ) {
$a = shortcode_atts( array(
'class-paper' => 'abcjs-paper',
'class-audio' => 'abcjs-audio',
'params' => '{}',
'options' => '{}',
'file' => '',
'number_of_tunes' => '1',
'animate' => false,
'qpm' => 'undefined'
), $atts );
if ($a['options'] == '{}')
$a['options'] = $a['params'];
$content2 = get_abc_string($a['file'], $content);
$ret = construct_divs2($a['number_of_tunes'], 'paper', $a['class-paper'], 'midi', $a['class-audio']);
$output = $ret['output'];
$ids = $ret['ids'];
$idsAudio = $ret['ids2'];
$animateCallback = "";
$animate = "null";
if ($a['animate']) {
$animateCallback = getCursorControl($ids);
$animate = "cursorControl";
}
$output = $output . openJsSection() .
$animateCallback .
'var visualIds = [' . $ids . "];\n" .
'var params = ' . $a['options'] . ";\n" .
'var abc = "' . $content2 . '".replace(/\x01/g,"\n");' . "\n" .
'var visualObjs = ABCJS.renderAbc(visualIds, abc, params);' . "\n" .
'var midiIds = [' . $idsAudio . "];\n" .
getSynthInit($idsAudio, $a['options'], $animate) . "\n" .
closeJsSection();
return $output;
}
add_shortcode( 'abcjs-audio', 'abcjs_create_audio' );
function openJsSection() {
$section = <<
(function () {
EOD;
return $section;
}
function closeJsSection() {
$section = <<
EOD;
return $section;
}
function getSynthInit($ids, $options, $cursorControl) {
$synth = << '',
'class' => 'abc-paper',
'options' => '{}',
), $atts);
$ret = construct_divs(1, 'editor', $a['class']);
$ids = $ret['ids'];
$divs = $ret['output'];
$textarea = $a['textarea'];
$options = $a['options'];
$editor = <<