1+ // Direct test script for piano multi-samples
2+ // Copy and paste this entire script into your browser console while in Music Blocks
3+
4+ ( function ( ) {
5+ console . log ( "%c🎹 PIANO MULTI-SAMPLE DIRECT TEST 🎹" , "background: #FF5722; color: white; font-size: 16px; padding: 5px;" ) ;
6+
7+ // First, verify that the samples are different
8+ if ( typeof verifyPianoMultiSamples === 'function' ) {
9+ console . log ( "Running sample verification..." ) ;
10+ const result = verifyPianoMultiSamples ( ) ;
11+ console . log ( "Verification result:" , result ) ;
12+ } else {
13+ console . warn ( "verifyPianoMultiSamples function not found. Make sure piano_multi.js is loaded." ) ;
14+ }
15+
16+ // Check if we're in Music Blocks with access to Tone.js
17+ if ( typeof Tone === 'undefined' ) {
18+ console . error ( "Tone.js not found. This test must be run within Music Blocks." ) ;
19+ return ;
20+ }
21+
22+ // Make sure Tone.js is started
23+ Tone . start ( ) . then ( ( ) => {
24+ console . log ( "%c✅ Tone.js audio context started" , "color: green; font-weight: bold;" ) ;
25+ runTest ( ) ;
26+ } ) . catch ( err => {
27+ console . error ( "Failed to start Tone.js:" , err ) ;
28+ } ) ;
29+
30+ function runTest ( ) {
31+ // Find the instrument name that's using piano_multi
32+ let isPianoMultiRegistered = false ;
33+ let instrumentName = "" ;
34+
35+ if ( typeof instrumentsSource !== 'undefined' ) {
36+ for ( const key in instrumentsSource ) {
37+ if ( instrumentsSource [ key ] [ 1 ] === "piano_multi" ) {
38+ isPianoMultiRegistered = true ;
39+ instrumentName = key ;
40+ break ;
41+ }
42+ }
43+
44+ console . log ( "%c🔍 Checking if piano_multi is registered:" , "font-weight: bold;" ) ;
45+ console . log ( isPianoMultiRegistered ?
46+ `✅ piano_multi is registered as "${ instrumentName } "` :
47+ "❌ piano_multi is NOT registered in instrumentsSource" ) ;
48+ } else {
49+ console . warn ( "instrumentsSource not found. Can't check if piano_multi is registered." ) ;
50+ }
51+
52+ // Test playing notes in different registers to verify sample selection
53+ console . log ( "%c🎵 Testing piano multi-sample note selection:" , "font-weight: bold;" ) ;
54+
55+ // Create a test synth with our samples
56+ const testSynth = new Tone . Sampler ( {
57+ "C2" : PIANO_C2_SAMPLE ( ) ,
58+ "C4" : PIANO_C4_SAMPLE ( ) ,
59+ "C6" : PIANO_C6_SAMPLE ( )
60+ } ) . toDestination ( ) ;
61+
62+ // Define test notes in different registers - updated to match the corrected boundaries
63+ const testNotes = [
64+ { note : "C2" , register : "low" , expectedSample : "C2" } ,
65+ { note : "G2" , register : "low" , expectedSample : "C2" } ,
66+ { note : "B2" , register : "low" , expectedSample : "C2" } , // B2 is MIDI 47, last note using C2 sample
67+ { note : "C3" , register : "middle" , expectedSample : "C4" } , // C3 is MIDI 48, first note using C4 sample
68+ { note : "G4" , register : "middle" , expectedSample : "C4" } ,
69+ { note : "B4" , register : "middle" , expectedSample : "C4" } , // B4 is MIDI 71, last note using C4 sample
70+ { note : "C5" , register : "high" , expectedSample : "C6" } , // C5 is MIDI 72, first note using C6 sample
71+ { note : "G6" , register : "high" , expectedSample : "C6" } ,
72+ { note : "C7" , register : "high" , expectedSample : "C6" }
73+ ] ;
74+
75+ // Create a table to track results
76+ const results = [ ] ;
77+
78+ // Play each note with a delay
79+ testNotes . forEach ( ( test , index ) => {
80+ setTimeout ( ( ) => {
81+ const noteNum = Tone . Frequency ( test . note ) . toMidi ( ) ;
82+ console . log ( `%cPlaying ${ test . note } (MIDI: ${ noteNum } ) in ${ test . register } register...` , "color: blue;" ) ;
83+
84+ // Play the note
85+ testSynth . triggerAttackRelease ( test . note , 0.5 ) ;
86+
87+ // Record which sample was used
88+ let actualSample ;
89+ // Use the same boundaries as in synthutils.js
90+ if ( noteNum <= 47 ) { // Notes up to B2
91+ actualSample = "C2" ;
92+ } else if ( noteNum <= 71 ) { // Notes from C3 to B4
93+ actualSample = "C4" ;
94+ } else { // Notes C5 and above
95+ actualSample = "C6" ;
96+ }
97+
98+ const result = {
99+ note : test . note ,
100+ midi : noteNum ,
101+ register : test . register ,
102+ expectedSample : test . expectedSample ,
103+ actualSample : actualSample ,
104+ correct : actualSample === test . expectedSample
105+ } ;
106+
107+ results . push ( result ) ;
108+ console . log ( `%c${ result . correct ? '✅' : '❌' } Used ${ actualSample } sample (${ result . correct ? 'correct' : 'incorrect' } )` ,
109+ result . correct ? "color: green;" : "color: red; font-weight: bold;" ) ;
110+
111+ // Show final results table after all notes have played
112+ if ( index === testNotes . length - 1 ) {
113+ setTimeout ( ( ) => {
114+ console . log ( "%c📊 FINAL TEST RESULTS:" , "background: #2196F3; color: white; font-size: 14px; padding: 3px;" ) ;
115+ console . table ( results ) ;
116+
117+ const allCorrect = results . every ( r => r . correct ) ;
118+ console . log ( `%c${ allCorrect ? '✅ ALL TESTS PASSED!' : '❌ SOME TESTS FAILED!' } ` ,
119+ `background: ${ allCorrect ? '#4CAF50' : '#F44336' } ; color: white; font-size: 14px; padding: 3px;` ) ;
120+
121+ if ( allCorrect ) {
122+ console . log ( "%c🎉 The piano multi-sample implementation is working correctly! The system is using the appropriate sample for each note range and not just transposing a single sample." ,
123+ "color: green; font-weight: bold;" ) ;
124+ } else {
125+ console . log ( "%c⚠️ There may be an issue with the piano multi-sample implementation. Some notes are not using the expected sample." ,
126+ "color: red; font-weight: bold;" ) ;
127+ }
128+ } , 500 ) ;
129+ }
130+ } , index * 700 ) ; // 700ms between notes
131+ } ) ;
132+ }
133+ } ) ( ) ;
0 commit comments