### Create a Chord Instance Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20chord%20type An example of creating a chord instance directly by providing a list of notes, intervals, and start time. ```Python chord(notes=[C6, E6, G6, A6], interval=[0, 0, 0, 0], start_time=0) ``` -------------------------------- ### MusicPy play and export chord examples Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module Demonstrates playing and exporting individual chords using the MusicPy daw. Examples show how to specify the chord, channel, and BPM for playback and export. ```python new_song.play(C('C')) # play a C major chord using the daw object with loaded instruments of channel 1 new_song.play(C('C'), 2) # do the same thing except using channel 2 new_song.play(C('C'), 2, bpm=165) # do the same thing except using channel 2 with BPM 165 new_song.export(C('C'), mode='wav') # export a C major chord as a wav file, using instruments of channel 1 new_song.export(C('C'), mode='mp3') # export a C major chord as a mp3 file, using instruments of channel 1 new_song.export(C('C'), channel_num=2, mode='wav', filename='my first song.wav') # export a C major chord as # a wav file, using instruments of channel 2, the name of the exported wav file is "my first song.wav" ``` -------------------------------- ### Install MusicPy Source: https://musicpy.readthedocs.io/en/latest/Introduction Installs the MusicPy library using pip. Ensure Python version 3.7 or higher is installed. For Linux, specific pygame and freepats versions might be required for audio playback. ```bash pip install musicpy ``` -------------------------------- ### MusicPy play and export piece examples Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module Illustrates playing and exporting complex musical pieces defined as 'piece' types. Examples cover creating musical parts, combining them, adding drums, and then playing or exporting the complete composition. ```python rule1 = lambda x: x % (1 / 8, 1 / 8) @ [1, 2, 3, 2] part1 = rule1(C('D#', 5)) | rule1(C('F', 5)) | rule1(C('Gm', 5)) * 2 part1_bass = chord('D#3[.2;.], F3[.2;.], G3[.2;.], G3[.4;.], F3[.4;.]') part1_harmony = part1_bass + perfect_fifth drums = drum('K, H, S, H, r:2').notes drums.set_volume(80) current_song = P([part1 * 2 | (part1 + database.octave) * 2, part1_bass * 4, part1_harmony * 2, drums * 4], bpm=120, start_times=[0, 0, 4, 4.03], daw_channels=[0, 1, 1, 2]) new_song.play(current_song) # play the piece type current_song new_song.export(current_song, filename='my first song.wav') # export the piece type current_song to # a wav file named "my first song.wav" new_song.export(current_song, action='play') # this will play the piece type current_song, # but firstly compiled it to an audio object, this is different from play function which is # playing on the fly, this method will be slower to start playing but will have more stable playing ``` -------------------------------- ### J-Rock Intro Style with Guitar, Bass, and Strings (MusicPy) Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%202 Composes a J-rock intro featuring layered guitar riffs, bass lines, and string parts. This example utilizes advanced chord manipulation, rhythmic patterns, and instrument layering for a dynamic sound. ```python guitar = ((C('Cmaj7')@1)@[1,2,3,4,1,2,3,2] | (C('Fmaj7',3)^2)@[1,2,3,4,1,2,3,2] | (C('G7sus',3)^2)@[1,2,3,4,1,2,3,2] | C('Am11',3)@[1,2,4,6,1,4,6,4] | (C('Cmaj7')@1)@[1,2,3,4,1,2,3,2] | C('Fadd9',3).up(2,1)@[1,2,3,4,1,2,3,2] | (C('G7sus',3)^2)@[1,2,3,4,1,2,3,2] | C('Am11',3)@[1,2,4,6,1,4,6,4]) % (1/2,1/8) bass1 = chord('D2[.8;.],E2[.8;.],D2[.8;.],E2[1;.], F2[1;.], G2[1;.], A1[.2;.], A2[.8;.], G2[.8;.], E2[.8;.], D2[.8;.]') bass2 = (chord('E2')*8 + chord('F1')*8 + chord('G1')*8 + chord('A1,A1,E2,A1,A2,A1,G2,D2')) % (1/8,1/8) * 4 bass = bass1 | bass2 string1 = chord('B5[1;.],C6[1;.],D6[.2;.],E6[.2;.], F6[.2;.],E6[.2;.],C6[1;.],B5[.2;.],C6[.2;.],G5[1;.], F5[.2;.],E5[.2;.],C5[1;.],D5[.4;.],E5[.4;.],F5[.4;.], E5[.4;.],D5[.2;.],G5[.2;.],E5[1;.]') play(piece([guitar * 3, bass, string1], [28, 34, 49], 135, [0, 4-3/8, 12])) ``` -------------------------------- ### Melody Example in C Major Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20scale%20type Demonstrates generating a melody in C major using the get method with a string representing scale degrees and durations. The output shows the resulting chord object with notes and intervals. ```python >>> S('C major').get('1,1,5,5,6,6,5,-,4,4,3,3,2,2,1,-') chord(notes=[C4, C4, G4, G4, A4, A4, G4, F4, F4, E4, ...], interval=[1/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/4, 1/8, 1/8, 1/8, ...], start_time=0) ``` -------------------------------- ### Create 6451 chord configuration with electric piano in Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%201 Implements a 6451 chord configuration with an electric piano sound. This example refines the chord generation by specifying a different numerical sequence for the chord structure and adjusting inversions. ```python q = scale('C4', 'major') r = q%(64516458, 1/2, 0.3/4, 5) r = [i.omit(7).inversion_highest(2) for i in r] play(r*2, bpm=80, instrument=5) ``` -------------------------------- ### Melody Example with Octave and Interval Changes Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20scale%20type Demonstrates generating a melody with octave and interval changes using the get method. It shows how to set octaves ('o4'), specify note durations within the string (e.g., '1[.8.;.]'), and group notes played simultaneously (e.g., '6;3.1;1.1'). ```python >>> S('A#5 major').get('1,7,5,3,2,3[.16;.], o4, 1[.8.;.],7,1,-, o3, 6;3.1;1.1, 6;3.1;1.1, 5;7.1;2.1, 5;7.1;2.1, 6;3.1;1.1, -') chord(notes=[A#5, A5, F5, D5, C5, D5, A#4, A4, A#4, G3, ...], interval=[1/8, 1/8, 1/8, 1/8, 1/8, 1/16, 3/16, 1/8, 1/4, 0, ...], start_time=0) ``` -------------------------------- ### Generate another 6451 chord configuration with harp tone in Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%201 Presents an alternative 6451 chord configuration with a harp tone, similar to the previous example but with different inversion settings. It highlights the flexibility in manipulating chord structures. ```python q = scale('C4', 'major') r = q%(6451, 0.5/4, 0.3/4, 5) r = [i.omit(7).inversion_highest(2) for i in r] play(r*2, bpm=80, instrument=47) ``` -------------------------------- ### Create 6451 chord configuration with harp tone in Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%201 Implements a 6451 chord configuration with a harp tone. This example demonstrates generating scales, applying chord structures (`%` operator), omitting notes, and inverting chords using Musicpy's functions. ```python q = scale('C4', 'major') r = q%(6451, 0.5/4, 0.3/4, 5) r = [i.omit(5).inversion_highest(3) for i in r] play(r*2, bpm=80, instrument=47) ``` -------------------------------- ### Export and Play Audio Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module This section details the `export` and `play` functions, their parameters, and usage examples for different audio actions and types. ```APIDOC ## POST /export ### Description Exports or plays musicpy data structures. Can convert to an AudioSegment instance for playback or further use. ### Method POST ### Endpoint /export ### Parameters #### Query Parameters - **mode** (string) - Optional - The audio format for export (e.g., 'wav', 'mp3', 'ogg'). - **action** (string) - Optional - 'export' (default) to save to file, 'play' to play the audio, 'get' to return an AudioSegment instance. - **filename** (string) - Optional - The name for the exported audio file. - **channel_num** (integer) - Optional - The 0-based index of the channel to use for export, relevant for note or chord types. - **bpm** (number) - Optional - The tempo (Beats Per Minute) for exporting the audio. If not specified, the DAW's default BPM is used. - **length** (number) - Optional - Similar to the `play` function parameter. - **extra_length** (number) - Optional - Similar to the `play` function parameter. - **track_lengths** (array) - Optional - Similar to the `play` function parameter. - **track_extra_lengths** (array) - Optional - Similar to the `play` function parameter. - **export_args** (object) - Optional - Dictionary of keyword arguments for audio export format settings, referencing pydub's `AudioSegment.export` parameters. - **show_msg** (boolean) - Optional - If True, prints rendering progress. - **soundfont_args** (object) - Optional - Dictionary of keyword arguments for rendering audio when using SoundFont instruments, corresponding to `sf2_loader.export_chord` arguments. ### Request Example ```json { "obj": "path/to/your/music_data_structure", "mode": "mp3", "action": "export", "filename": "output.mp3", "bpm": 120, "show_msg": true } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating successful export or playback. #### Response Example ```json { "message": "Audio exported successfully to output.mp3" } ``` ## POST /play ### Description Plays musicpy data structures directly using the DAW's playback functionality. ### Method POST ### Endpoint /play ### Parameters #### Query Parameters - **obj** (any) - The musicpy data structure to play (e.g., chord, piece). - **channel_num** (integer) - Optional - The 0-based index of the channel to use for playback. - **bpm** (number) - Optional - The tempo (Beats Per Minute) for playback. ### Request Example ```json { "obj": "C('C')", "channel_num": 1, "bpm": 165 } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating playback has started. #### Response Example ```json { "message": "Playback started" } ``` ## POST /stop_playing ### Description Stops all currently playing sounds managed by the DAW. ### Method POST ### Endpoint /stop_playing ### Response #### Success Response (200) - **message** (string) - Confirmation message that playback has stopped. #### Response Example ```json { "message": "Playback stopped" } ``` ``` -------------------------------- ### Import musicpy daw module Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module Imports all functions and classes from the musicpy.daw module. Ensure all required dependencies are installed beforehand. ```python from musicpy.daw import * ``` -------------------------------- ### Construct a Track Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20Quickstart%20and%20Cheat%20Sheet Defines a single musical track with content, instrument, and start time. ```python track(content=C('C'), instrument=1, start_time=0) ``` -------------------------------- ### Chord Progressions from Lydian Mode with Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%202 Generates chord progressions in major keys borrowed from the Lydian mode. This example creates a primary chord sequence and a secondary melodic line, combining them for the final song. ```python a = (C('Cmaj7') | C('D7') | C('Fmaj7',3) | C('Cmaj7/-3')) % (1, 1/4) * 4 b_duration = [3/4, 1/8, 1/8, 3/4, 1/8, 1/8, 1/4, 1/4, 1/2, 1/2, 1/2] b_interval = b_duration b = (chord('G5, F5, E5, F5, E5, D5, E5, D5, C5, B4, G4') % (b_duration, b_interval)) * 2 song = build([a, 5, 0], [b, 49, 4], bpm=165) play(song) ``` -------------------------------- ### Create horror ambient music with orchestral tones in Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%201 Composes horror ambient music using orchestral tones. This example demonstrates generating chords, manipulating their inversions and reversals, and layering them to create an eerie atmosphere. ```python a = get_chord('B4','maj9').sort([2,3,4,1,5]) b = get_chord('B4','maj9').sort([2,3,4,1,5,2]) q = a + a[1:-1].reverse_chord() q2 = b + b[3:-1].reverse_chord() play((q.set(1/8,1/8) + q2.set(1/8,1/8))*2, 100, instrument=50) ``` -------------------------------- ### Playing and Stopping Audio Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module Provides examples of using the `play()` and `stop()` functions for audio playback, similar to their usage with sound objects. ```python pitch_1.play() pitch_1.stop() ``` -------------------------------- ### Suspenseful Atmosphere Music Composition in Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%203 This example creates music with a suspenseful atmosphere using `get_chord` and `translate` functions. It defines chord progressions and applies rhythmic variations, then plays the result at a given BPM with an instrument index. ```python a = get_chord('bb2', 'm11') % (1/2,1/8) @ [1,3,5,4.1,2.2,6.1,5.1,4.1] b = get_chord('g2', 'M9#11') % (1/2,1/8) @ [1,3,5,4.1,2.2,6.1,5.1,4.1] c = get_chord('gb2', '13sus') % (1/2,1/8) @ [1,3,4,5,2.1,6,4.1,5.1] play(a * 4 | (a-2) * 2 | b | c, 135, i=5) # 另一个版本 play((a * 4 | (a-2) * 2 | b | c) * 2, 100, i=49) ``` -------------------------------- ### Get Chord Voicing and Play Arpeggiated Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20chord%20type Demonstrates how to get a specific chord voicing and play it with fast arpeggios. It shows the use of `get_voicing` for chord arrangement and the modulo operator for arpeggiation. ```python >>> C('Cm11').get_voicing([1,5,9,3,11,7]) # C minor eleventh chord according to root, 5th, 9th, 3rd, 11th, 7th degree, # and redistribute the octaves of all notes according to the rule that the following notes are higher than the preceding notes chord(notes=[C4, G4, D5, D#5, F5, A#5], interval=[0, 0, 0, 0, 0, 0], start_time=0) # Return to the voicing chord type of the C minor eleven chord arranged according to the specified chord vocal position list play(C('Cm11').get_voicing([1,5,9,3,11,7])% (1, 1/8), 150) # Play with fast arpeggios ``` -------------------------------- ### Create Game BGM (Speedcore, 500BPM) with Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%202 Composes high-tempo (500 BPM) background music for a game, incorporating speedcore elements. This example includes piano, bass, and random drum patterns. ```python a = C('Cm9',4,1/2,1/8) @ [1,3,5,2.1,4.1] | 3/8 piano = a * 4 | (a + 3) * 4 bass = (chord('C2, C2, G2, C2, A#2, C2, C3, C2') * 4) % (1/8, 1/8) bass += (bass + 3) import random drum = concat([chord(random.choice(['D2','E2','G2'])) for i in range(64)]) % (1/8, 1/8) play(P([piano, bass, drum], [1, 34, 1], 500, [0, 0, 0], channels=[1,2,9]) * 2) ``` -------------------------------- ### Playing Notes and Chords Source: https://musicpy.readthedocs.io/en/latest/Useful%20functionality Demonstrates how to play individual notes and chords with specified durations and intervals. Includes examples of playing broken chords and reversing chord sequences. ```APIDOC ## POST /api/play/note ### Description Plays a single musical note. ### Method POST ### Endpoint /api/play/note ### Parameters #### Request Body - **note_name** (string) - Required - The name of the note (e.g., 'A', 'C#'). - **octave** (integer) - Required - The octave of the note. - **duration** (integer) - Optional - The duration of the note in beats (defaults to 1). - **wait** (boolean) - Optional - Whether to wait for playback to finish (defaults to False). ### Request Example ```json { "note_name": "A", "octave": 5, "wait": true } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example ```json { "message": "Note A5 played successfully." } ``` ## POST /api/play/chord ### Description Plays a musical chord, optionally with specified intervals and duration. ### Method POST ### Endpoint /api/play/chord ### Parameters #### Request Body - **root_note** (string) - Required - The root note of the chord (e.g., 'C', 'G'). - **chord_type** (string) - Required - The type of chord (e.g., 'maj7', 'm7'). - **intervals** (integer) - Optional - The spacing between notes in the chord (defaults to 1). - **duration** (integer) - Optional - The duration of the chord in beats (defaults to 1). - **bpm** (integer) - Optional - The beats per minute for playback (defaults to 120). ### Request Example ```json { "root_note": "C", "chord_type": "maj7", "intervals": 1, "duration": 2, "bpm": 150 } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example ```json { "message": "Chord Cmaj7 played successfully." } ``` ## POST /api/play/sequence ### Description Plays a sequence of musical elements, which can include multiple chords or notes. ### Method POST ### Endpoint /api/play/sequence ### Parameters #### Request Body - **sequence** (array) - Required - An array of note or chord objects to play. - Each object can have `type` ('note' or 'chord'), `name`/`root_note`, `octave`/`chord_type`, `duration`, `intervals`, `reverse` (for chords), etc. - **bpm** (integer) - Required - The beats per minute for playback. ### Request Example ```json { "sequence": [ { "type": "chord", "root_note": "D", "chord_type": "m7", "duration": 1, "intervals": 1 }, { "type": "chord", "root_note": "D", "chord_type": "m7", "duration": 1, "intervals": 1 }, { "type": "chord", "root_note": "E", "chord_type": "maj9", "duration": 0.5, "intervals": 0.5, "reverse": true } ], "bpm": 100 } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example ```json { "message": "Sequence played successfully." } ``` ``` -------------------------------- ### MusicPy: Chord Function Examples Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20chord%20type Illustrates the usage of the 'chord' function in MusicPy with various relative pitch syntaxes for constructing chords. Supports semitone and octave adjustments. ```python example = chord('A#4, +7, +10, +1o2, +1o3, +1o5, +1o3, +1o2', default_interval=1/8, default_duration=1/8) >> example chord(notes=[A#4, F5, G#5, C6, C#6, D#6, C#6, C6], interval=[1/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8, 1/8], start_time=0) ``` ```python example2 = chord('C5, ++2, ++2, ++1, ++2', default_interval=1/8, default_duration=1/8) >> example2 chord(notes=[C5, D5, E5, F5, G5], interval=[1/8, 1/8, 1/8, 1/8, 1/8], start_time=0) ``` ```python example3 = chord('C5(+1)[.8;.], E5, G5', default_interval=1/8, default_duration=1/8) >>> example3 chord(notes=[C#5, E5, G5], interval=[1/8, 1/8, 1/8], start_time=0) ``` -------------------------------- ### Recreate The Trooper - Iron Maiden with Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module%20composition%20code%20examples Composes a piece inspired by 'The Trooper' by Iron Maiden using the musicpy daw module. This example demonstrates loading a SoundFont file, creating complex guitar and bass lines using pattern translations, and adding drum beats. The function then plays the generated music. ```python from musicpy.daw import * current_daw = daw(3) current_daw.load(0, 'Arachno.sf2') guitar11 = translate('D, G, D, a:1/8;., E[l:1/4; i:.], G[l:1/8; i:.] | F#, G, F#, G, a:1/16;., n:1, B[l:1/8; i:.], G[l:1/8; i:.], u:1, G[l:1/8; i:.], E[l:1/8; i:.], E[l:1/4; i:.]') * 2 guitar12 = translate('D, G, D, a:1/8;., E[l:1/4; i:.]') guitar1_part = guitar11 * 2 + guitar12 bass1 = translate('D2, G2, D2, a:1/8;.') bass2 = translate('E2[l:.4; i:.] | E2[l:.16; i:.; r:2], E2[l:.8; i:.], n:1, u:1, r:4, E2[l:.16; i:.; r:2], D2[l:.8; i:.; r:3]') bass3 = translate('C2[l:.4; i:.] | C2[l:.16; i:.; r:2], C2[l:.8; i:.], n:1, u:1, r:4, C2[l:.16; i:.; r:2], D2[l:.8; i:.; r:3]') bass4 = translate('D2, G2, D2, a:1/8;., E2[l:.4; i:.]') bass_part = bass1 + bass2 * 2 + bass3 + bass2[:-3] + bass4 drum_part = drum('C;S;K[l:1/4; i:.], H, S, H, K, H, S, H, K, H, S, H, S[r:3], r:4, C;S;K[l:1/4; i:.]').notes guitar21 = translate('F#, G, F#, a:1/8;., G[l:1/4; i:.], B4[l:1/8; i:.] | A, B, A, B, a:1/16;., n:1, D5[l:1/8; i:.], B4[l:1/8; i:.], u:1, B[l:1/8;i:.], G[l:1/8; i:.], G[l:1/4; i:.]') * 2 guitar22 = translate('F#, G, F#, a:1/8;., G[l:1/4; i:.]') guitar2_part = guitar21 * 2 + guitar22 guitar2_part.set_volume(80) result = P(tracks=[guitar1_part, bass_part, drum_part, guitar2_part], instruments=[31, 34, 1, 31], channels=[0, 1, 9, 2], daw_channels=[0, 0, 0, 0], start_times=[0, 0, 3/8, 0], bpm=165) current_daw.play(result) ``` -------------------------------- ### Relaxed Urban Ambient Music with Transposition (MusicPy) Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%202 Generates relaxed urban-style ambient music, incorporating transposition and intricate part arrangements. This example features multiple instrument layers (piano, bass, string, oboe) with detailed rhythmic and melodic structuring. ```python a = C('CM9') @ [1,3,5,2.1,4.1,5.1] % (1, 1/8) | 1/4 bass = chord('C2, A#1') % (1, 1) * 4 piano = (a | a - 2) * 4 string = C('CM9', 4) % (1, 0) string |= string - 2 string.set_volume(50) string *= 4 oboe1 = chord('B5[.16;.16], C6[.16;.16], D6[1;1], C6[.2;.2], E6[.2;.2], D6[1;1], C6[7/8;7/8]') part1 = P([piano | piano-4, bass | bass-4, string | string-4, oboe1 | oboe1-4], [9, 34, 49, 'Pan Flute'], 90, [0, 0, 0, 3+7/8]) play(part1) ``` -------------------------------- ### Construct a Piece Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20Quickstart%20and%20Cheat%20Sheet Builds a musical piece comprising multiple tracks with specified instruments, content, and start times. Also includes tempo (BPM) settings. ```python build(track(content=C('C'), instrument=1, start_time=0), track(content=C('D'), instrument=47, start_time=1), bpm=150) P(tracks=[C('C'), C('D')], instruments=[1, 47], start_times=[0, 1], bpm=150) ``` -------------------------------- ### Scale Class Initialization Source: https://musicpy.readthedocs.io/en/latest/Data%20Structures%20of%20musicpy Details the constructor for the `scale` class in MusicPy, explaining the parameters used to define a scale, including the starting note, mode, intervals, and a list of notes. ```python scale(start=None, mode=None, interval=None, notes=None) ``` -------------------------------- ### Piece Class Initialization Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20piece%20type Demonstrates the initialization of the piece class with multiple tracks, specifying instruments, BPM, start times, and track names. ```APIDOC ## piece Class Initialization ### Description Initializes a musical piece with multiple tracks, allowing for detailed configuration of each track, including its instrument, start time, and name. ### Method `piece()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **tracks** (list) - Required - A list of chord types for each track. - **instruments** (list) - Optional - A list of instruments for each track. Can be MIDI numbers (0-127) or instrument names. - **bpm** (int) - Optional - The tempo of the piece in beats per minute. Defaults to 120. - **start_times** (list) - Optional - A list of start times for each track in bars. Defaults to all tracks starting at 0. - **track_names** (list) - Optional - A list of names for each track. - **channels** (list) - Optional - A list of MIDI channel numbers (0-based) for each track. Crucial for drum tracks (channel 9). - **name** (str) - Optional - The name of the piece. - **pan** (list) - Optional - A list of pan messages for each channel. - **volume** (list) - Optional - A list of volume messages for each channel. - **other_messages** (list) - Optional - A list of other MIDI messages for the piece. - **daw_channels** (list) - Optional - Specifies the corresponding DAW channel number for each track. ### Request Example ```python # Example chord definitions (assuming chord class is defined elsewhere) # C1 = chord('G4, D5, B5, F#5') % (1, 1/8) * 4 # C2 = (chord('C2, C2, G1, G1') % (1,1)) * 2 # C3 = (chord('F#6, G6') % (1/8, 1/8) * 8 | chord('A5, B5') % (1/8, 1/8) * 8) * 2 # C4 = chord('G3, G3, G3, G3') % ([3/8,1/8,1/4,1/4], [3/8,1/8,1/4,1/4]) * 4 new_piece = piece(tracks=[C1, C2, C3, C4], instruments=['Acoustic Grand Piano', 'Electric Bass (finger)', 'Orchestral Harp', 'Synth Drum'], bpm=120, start_times=[0, 2, 2, 6], track_names=['piano', 'bass', 'harp', 'drum'], channels=[0, 1, 2, 9]) # Example: Drum track on channel 9 ``` ### Response #### Success Response (200) Returns a `piece` object representing the configured musical composition. #### Response Example ```json { "piece": { "BPM": 120, "tracks": [ { "track_name": "piano", "instrument": "Acoustic Grand Piano", "start_time": 0, "content": "chord(notes=[G4, D5, B5, F#5, ...], interval=[1/8, ...], start_time=0)" }, { "track_name": "bass", "instrument": "Electric Bass (finger)", "start_time": 2, "content": "chord(notes=[C2, C2, G1, G1, ...], interval=[1, ...], start_time=0)" }, { "track_name": "harp", "instrument": "Orchestral Harp", "start_time": 2, "content": "chord(notes=[F#6, G6, ...], interval=[1/8, ...], start_time=0)" }, { "track_name": "drum", "instrument": "Synth Drum", "start_time": 6, "content": "chord(notes=[G3, G3, ...], interval=[3/8, ...], start_time=0)" } ] } } ``` ``` -------------------------------- ### Chord Construction with Normalization Example Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20chord%20type Demonstrates how the 'chord' function normalizes note pitches and arrangements when given a string without explicit pitches. ```python chord('C, E, G, B, D') ``` ```python chord(notes=[C4, E4, G4, B4, D5], interval=[0, 0, 0, 0, 0], start_time=0) ``` -------------------------------- ### Create a C Major Seventh Chord Source: https://musicpy.readthedocs.io/en/latest/Data%20Structures%20of%20musicpy Demonstrates how to create a chord instance by providing a list of note names as strings. This example creates a Cmaj7 chord. ```python Cmaj7 = chord(['C5', 'E5', 'G5', 'B5']) ``` -------------------------------- ### Play Chords Source: https://musicpy.readthedocs.io/en/latest/Useful%20functionality Demonstrates playing complex chords and sequences. Includes examples of creating major seventh and major ninth chords, manipulating their duration and order, and playing them at specified BPMs. ```python b = get_chord('C', 'maj7', intervals=1, duration=2) play(b, 150) ``` ```python c = get_chord('D', 'm7', intervals=1)*2 + get_chord('E', 'maj9', intervals=0.5).reverse() play(c, 100) ``` -------------------------------- ### Merge Tracks and Get Piece Info Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20piece%20type Merges tracks from a piece type into a single track and retrieves the tempo (BPM) and start time of the piece. The start time is returned in bars. ```python b, bpm, start_time = a.merge() # Use the built-in function merge of the piece type to get the tuple of # (merged chord type, bpm, start time), with the start time in bars ``` -------------------------------- ### Get Note with Interval API Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20scale%20type Calculates a note based on an interval from a starting note within a specified scale. ```APIDOC ## GET /scale/get_note_with_interval ### Description Calculates a resulting note by applying an interval to a current note within a scale. ### Method GET ### Endpoint /scale/get_note_with_interval ### Parameters #### Query Parameters - **scale** (string) - Required - The scale context (e.g., 'C major'). - **current_note** (string) - Required - The starting note (can be a string or note type). - **interval** (integer) - Required - The interval in scale degrees to move from the current note, starting from 1. - **standard** (boolean) - Optional - If True, ensures the resulting note is in standard notation. Defaults to False. ### Request Example ```json { "scale": "C major", "current_note": "C4", "interval": 3 } ``` ### Response #### Success Response (200) - **result_note** (string) - The calculated note. #### Response Example ```json { "result_note": "E4" } ``` ``` -------------------------------- ### Create a Major Scale with Specific Intervals Source: https://musicpy.readthedocs.io/en/latest/Data%20Structures%20of%20musicpy Shows how to define a scale using specific intervals, starting from a root note. This example constructs a C major scale with C5 as the root note. ```python scale('C5', interval=[2, 2, 1, 2, 2, 2, 1], mode='major') ``` -------------------------------- ### Create and Use an Effect Chain Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20daw%20module Demonstrates initializing an `effect_chain` with multiple effect instances and applying the chain to a MusicPy data structure. The example shows creating a mixer with delay, fade-in, and fade-out effects. ```python mixer1 = effect_chain(delay_effect(interval=0.2, unit=6), fade_in(duration=2000), fade_out(duration=3000)) # you pass in effect instances as positional arguments when initialize an effect_chain instance # the representation of an effect_chain instance >>> mixer1 [effect chain] effects: [effect] name: delay parameters: [(), {'interval': 0.2, 'unit': 6}] unknown arguments: {} [effect] name: fade in parameters: [(), {'duration': 2000}] unknown arguments: {} [effect] name: fade out parameters: [(), {'duration': 3000}] unknown arguments: {} # how to use an effect_chain instance on musicpy data structures Cmaj7_chord = C('Cmaj7') Cmaj7_chord = mixer1(Cmaj7_chord) # you can just simply apply the effect_chain instance on musicpy data structures by wrapping it up, # the return value is the same musicpy data structures with the effects stored ``` -------------------------------- ### Get Note with Interval in Scale (Python) Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20scale%20type Retrieves a note from a scale given a starting note and an interval. The method accounts for the scale's structure to determine the resulting note. It supports both positive and negative intervals. ```python from musicpy import * current_scale = S('C major') # Get note with a positive interval result1 = current_scale.get_note_with_interval('C4', 3) print(result1) # Expected output: E4 # Get note with a larger positive interval result2 = current_scale.get_note_with_interval('C4', 9) print(result2) # Expected output: D5 # Get note with a negative interval result3 = current_scale.get_note_with_interval('C4', -5) print(result3) # Expected output: F3 ``` -------------------------------- ### Get Note with Interval from Note in Scale (Python) Source: https://musicpy.readthedocs.io/en/latest/Basic%20syntax%20of%20scale%20type Calculates a note by applying an interval to a starting note within a scale. The `interval` parameter specifies the number of scale degrees to move. Requires a scale object created by `S`. ```python get_note_with_interval(current_note, interval, standard=False) # current_note: the note to be counted, can be a string or a note type # interval: the number of notes, starting from 1, 1 being one degree ``` -------------------------------- ### Rolling Star - Yui Composition in Musicpy Source: https://musicpy.readthedocs.io/en/latest/Musicpy%20composition%20code%20examples%20part%203 This snippet recreates the song 'Rolling Star' by Yui using Musicpy. It defines separate tracks for bass, guitar, drums, and synth, then combines them with specified BPM, channel assignments, and start times. ```python bass11 = translate('B1[l:.8; i:.; r:8], D2[l:.8; i:.; r:8], A1[l:.8; i:.; r:8], G1[l:.8; i:.; r:8]') bass12 = translate('G1[l:.8; i:.; r:6], A1[l:.8; i:.; r:2]') bass1 = bass11 * 2 | bass12 guitar11 = translate('B3[l:.4; i:.], D4[l:.8; i:.], E4[l:3/8; i:.], D4[l:.8; i:.], E4[l:.8; i:.]') guitar12 = guitar11.down(2, 0) guitar13 = guitar12.down(1, [1, 3]) guitar14 = translate('G3[l:.4; i:.], B3[l:.8; i:.], A4[l:5/8; i:.]') guitar1 = (guitar11|guitar12|guitar13|guitar14) * 2 drum11 = drum('C;K, H, S, H | K, H, S, H, r:5, C;K, H;S[r:2], PH, OH, S;OH[r:3]').notes drum12 = drum('C;K;H;S, H;S[r:2], PH, OH, S;OH[r:3]').notes drum1 = drum11 * 2 | drum12 synth11 = translate('D5[l:.8; i:.], B5[l:.8; i:.], r:16') synth1 = synth11 result = P([bass1, guitar1, drum1, synth1], [34, 31, 1, 91], bpm=165, channels=[0,1,9,2], start_times=[0,0,0,4]) play(result) ```