Просмотр исходного кода

Remote control: pattern lengths in whole bars, and the song's meter

#76 review. pattern.set and pattern.create took any lengthTicks in 0..1e9, while LYNX's own pattern length box
only sets 0 (automatic) or 1..999 whole bars. A length the UI cannot make reached Pattern Blocks and export. Now a
positive length must be a whole number of bars of the song's meter, 1..999; restating a pattern's current length
stays a no-op even when it is not a whole bar (imported projects).

Round 190fg gave the song a meter unit (Pattern::effectiveLength takes a Meter), so the remote pattern and
playlist rows compute effective lengths from the song's meter, and status / session.inspect report beatUnit and
barTicks next to beatsPerBar. Tests cover the bar bounds, a 6/8 song and a note longer than the requested bars.
Ka1-S 2 недель назад
Родитель
Сommit
600b79aeae

+ 10 - 5
docs/REMOTE_CONTROL.md

@@ -92,8 +92,10 @@ no credential.
 
 - `api.describe` lists this connection's supported operations, capabilities,
   protocol and frame limits.
-- `status` returns bounded project identity, revision, tempo, transport state,
-  position, current pattern and collection counts without copying the project.
+- `status` returns bounded project identity, revision, tempo, the meter
+  (`beatsPerBar`, `beatUnit` and `barTicks`, the length of one bar in ticks),
+  transport state, position, current pattern and collection counts without
+  copying the project. `session.inspect` reports the same meter fields.
 - `changes {fromRevision,limit?}` returns up to 128 observed revision changes and
   `gap`/`hasMore`. It is a bounded polling feed, not a push subscription or an
   exhaustive edit log. Several UI changes between observations may coalesce.
@@ -134,9 +136,12 @@ stopped playback/recording, and commits one remote-owned native undo step.
   metadata is preserved. A no-op does not advance revision or add history.
 
 Names contain 1–256 characters, include non-whitespace text and have no control
-characters. `lengthTicks` is an integer
-from zero through 1,000,000,000 at PPQ 960; zero follows the notes automatically.
-A positive length cannot end before an existing note. Creation/cloning select
+characters. `lengthTicks` (PPQ 960) is zero, which follows the notes
+automatically, or a whole number of bars of the song's meter from 1 through 999
+(`barTicks` from `status` times the bar count), the lengths LYNX's own pattern
+length box sets. Restating a pattern's current length is a no-op even when it is
+not a whole bar (an imported project). A positive length cannot end before an
+existing note. Creation/cloning select
 the new pattern. Playlist clips, Pattern Blocks across arrangements and channel
 Loop Starter references keep their source targets as indices shift. Length
 changes resize untrimmed full-pattern clips as in LYNX's UI; trimmed clips keep

+ 4 - 3
src/engine/RemoteControl.cpp

@@ -139,7 +139,7 @@ struct RemoteControl::Impl final : juce::Thread
             return object ({ { "project", object ({ { "id", text (s.projectId) }, { "name", projectName() },
                     { "path", projectPath ? projectPath() : juce::String() }, { "modified", engine.isModified() } }) },
                 { "appVersion", JUCE_APPLICATION_VERSION_STRING }, { "bpm", s.bpm }, { "beatsPerBar", s.beatsPerBar },
-                { "playing", t.playing.load() }, { "recording", engine.isRecording() },
+                { "beatUnit", s.beatUnit }, { "barTicks", (juce::int64) s.barTicks() }, { "playing", t.playing.load() }, { "recording", engine.isRecording() },
                 { "songMode", s.mode == PlayMode::song }, { "positionTicks", t.positionTicks.load() }, { "ppq", ppq },
                 { "currentPattern", s.currentPattern }, { "channelCount", (int) s.channels.size() },
                 { "patternCount", (int) s.patterns.size() }, { "canUndo", engine.canUndo() }, { "canRedo", engine.canRedo() },
@@ -175,7 +175,7 @@ struct RemoteControl::Impl final : juce::Thread
             for (std::size_t i = 0; i < s.patterns.size(); ++i)
                 patterns.add (object ({ { "index", (int) i }, { "patternId", text (uids::toHex (s.patterns[i].uid)) }, { "name", text (s.patterns[i].name) },
                                        { "lengthTicks", (juce::int64) s.patterns[i].lengthTicks },
-                                       { "effectiveLengthTicks", (juce::int64) s.patterns[i].effectiveLength (s.beatsPerBar) },
+                                       { "effectiveLengthTicks", (juce::int64) s.patterns[i].effectiveLength (s.meter()) },
                                        { "colour", (juce::int64) s.patterns[i].colour },
                                        { "noteCount", (int) s.patterns[i].notes.size() } }));
             for (std::size_t i = 0; i < s.playlist.size(); ++i)
@@ -189,7 +189,8 @@ struct RemoteControl::Impl final : juce::Thread
             return object ({ { "project", object ({ { "name", projectName() }, { "id", text (s.projectId) },
                                                               { "path", projectPath ? projectPath() : juce::String() }, { "modified", engine.isModified() } }) },
                             { "controlEpoch", controlEpoch }, { "appVersion", JUCE_APPLICATION_VERSION_STRING },
-                            { "bpm", s.bpm }, { "beatsPerBar", s.beatsPerBar }, { "playing", t.playing.load() }, { "recording", engine.isRecording() },
+                            { "bpm", s.bpm }, { "beatsPerBar", s.beatsPerBar }, { "beatUnit", s.beatUnit },
+                            { "barTicks", (juce::int64) s.barTicks() }, { "playing", t.playing.load() }, { "recording", engine.isRecording() },
                             { "songMode", s.mode == PlayMode::song }, { "positionTicks", t.positionTicks.load() },
                             { "ppq", ppq }, { "channels", channels }, { "mixer", mixer }, { "patterns", patterns },
                             { "currentPattern", s.currentPattern }, { "playlist", playlist },

+ 1 - 1
src/engine/RemotePatternControl.cpp

@@ -81,7 +81,7 @@ juce::var dispatchRemotePattern (AudioEngine& engine, const juce::String& operat
         properties->setProperty ("patternId", text (uids::toHex (pattern.uid)));
         properties->setProperty ("name", text (pattern.name));
         properties->setProperty ("lengthTicks", (juce::int64) pattern.lengthTicks);
-        properties->setProperty ("effectiveLengthTicks", (juce::int64) pattern.effectiveLength (engine.getSong().beatsPerBar));
+        properties->setProperty ("effectiveLengthTicks", (juce::int64) pattern.effectiveLength (engine.getSong().meter()));
         properties->setProperty ("patternCount", (int) engine.getSong().patterns.size());
         properties->setProperty ("changed", changed);
         return result;

+ 10 - 7
src/engine/RemotePatternEdits.h

@@ -7,7 +7,9 @@
 
 namespace lynx::remote_patterns
 {
-constexpr std::int64_t maxLength = 1000000000;
+constexpr std::int64_t maxLength = 1000000000; // (a bound on what a request may carry before its bars are checked)
+// The lengths LYNX's own pattern length box can set (round 190ha): 0 = automatic, else whole bars of the song's meter
+constexpr int maxBars = 999;
 constexpr int maxPatterns = 100000;
 struct Result { Song song; int index; };
 
@@ -27,17 +29,18 @@ inline void validateName (const std::optional<std::string>& name)
         || std::any_of (name->begin(), name->end(), [] (unsigned char c) { return c < 32 || c == 127; }))
         throw std::runtime_error ("name must contain visible text without control characters");
 }
-inline void validateLength (std::int64_t length)
+inline void validateLength (const Song& song, std::int64_t length)
 {
-    if (length < 0 || length > maxLength)
-        throw std::runtime_error ("lengthTicks must be in 0..1000000000 (0 is automatic)");
+    const auto bar = song.barTicks();
+    if (length < 0 || length > maxBars * bar || length % bar != 0)
+        throw std::runtime_error ("lengthTicks must be 0 (automatic) or a whole number of bars of the song's meter, 1..999 bars");
 }
 inline void validateCreate (const Song& song, int index, const std::optional<std::string>& name, std::int64_t length)
 {
     if (song.patterns.size() >= (std::size_t) maxPatterns || index < 0 || index > (int) song.patterns.size())
         throw std::runtime_error ("index must be an insertion position below the pattern limit");
     validateName (name);
-    validateLength (length);
+    validateLength (song, length);
 }
 inline void validateClone (const Song& song, int index, const std::optional<std::string>& name)
 {
@@ -53,9 +56,9 @@ inline bool metadataChanged (const Song& song, int index, const std::optional<st
     if (! name && ! length) throw std::runtime_error ("Supply name or lengthTicks");
     validateName (name);
     const auto& pattern = song.patterns[(std::size_t) index];
-    if (length)
+    if (length && *length != pattern.lengthTicks) // (restating the current length, whatever it is, stays a no-op)
     {
-        validateLength (*length);
+        validateLength (song, *length);
         if (*length > 0)
             for (const auto& note : pattern.notes)
                 if (note.start < 0 || note.length < 0 || note.start > *length || note.length > *length - note.start)

+ 1 - 1
src/engine/RemotePlaylistControl.cpp

@@ -177,7 +177,7 @@ juce::var inspect (const Song& song, const juce::var& args)
     }
     for (std::size_t i = 0; i < std::min (song.patterns.size(), maxSources); ++i)
         patterns.add (object ({ { "index", (int) i }, { "name", text (song.patterns[i].name) },
-                               { "length", (juce::int64) song.patterns[i].effectiveLength (song.beatsPerBar) } }));
+                               { "length", (juce::int64) song.patterns[i].effectiveLength (song.meter()) } }));
     for (std::size_t i = 0; i < song.channels.size() && (std::size_t) audio.size() < maxSources; ++i)
         if (song.channels[i].instrument == InstrumentKind::audioClip)
             audio.add (object ({ { "index", (int) i }, { "name", text (song.channels[i].name) }, { "mixer", song.channels[i].mixerTrack } }));

+ 16 - 1
tests/RemotePatternEditsTests.cpp

@@ -237,8 +237,22 @@ void metadataAndValidation()
     followPatternLengths (patternLengths (trimmed), resized.song);
     check (resized.song.playlist[0].length == 1500 && resized.song.arrangements[1].clips[0].length == 3840,
            "length following did not preserve trimmed clips or update stored arrangements");
-    check (remote_patterns::set (original, 0, {}, 960).song.patterns[0].lengthTicks == 960, "exact last note end was refused");
     check (remote_patterns::set (original, 0, {}, 0).song.patterns[0].lengthTicks == 0, "automatic length was refused");
+    // Round 190ha: only what LYNX's pattern length box can set, 0 (automatic) or 1..999 whole bars of the song's meter
+    check (remote_patterns::set (original, 0, {}, 999 * 3840).song.patterns[0].lengthTicks == 999 * 3840, "999 bars were refused");
+    for (const auto ticks : { std::int64_t (960), std::int64_t (1920), std::int64_t (3841), std::int64_t (1000) * 3840 })
+        refuses ([&] { remote_patterns::set (original, 0, {}, ticks); });
+    refuses ([&] { remote_patterns::create (original, {}, {}, 1920); });
+    auto compound = original; // 6/8: a bar is six eighths, 2880 ticks
+    compound.beatsPerBar = 6;
+    compound.beatUnit = 8;
+    check (remote_patterns::set (compound, 0, {}, 2880).song.patterns[0].lengthTicks == 2880
+           && remote_patterns::create (compound, {}, {}, 5760).song.patterns.back().lengthTicks == 5760, "whole 6/8 bars were refused");
+    refuses ([&] { remote_patterns::set (compound, 0, {}, 3840); });
+    auto longNote = original; // a whole-bar length still cannot end before a note
+    longNote.patterns[0].notes[0].length = 4000;
+    refuses ([&] { remote_patterns::set (longNote, 0, {}, 3840); });
+    check (remote_patterns::set (longNote, 0, {}, 2 * 3840).song.patterns[0].lengthTicks == 7680, "a length past the last note was refused");
 
     refuses ([&] { remote_patterns::create (original, -1); });
     refuses ([&] { remote_patterns::create (original, 3); });
@@ -252,6 +266,7 @@ void metadataAndValidation()
     refuses ([&] { remote_patterns::set (original, 0); });
     refuses ([&] { remote_patterns::set (original, 0, {}, 959); });
     refuses ([&] { remote_patterns::set (original, 0, "Rejected rename", 959); });
+    refuses ([&] { remote_patterns::set (original, 0, "Rejected rename", 1921); });
     refuses ([&] { remote_patterns::set (original, 2, "Missing"); });
     check (original.patterns[0].name == "Lead" && original.patterns[0].lengthTicks == 1920
            && original.playlist[0].length == 1920, "rejected metadata edit changed the original model");