0

Sorry if I'm not getting something right, I am new to web application development, I've been working on a project with Django and I wish I could do the following:

When the user clicks on a button an Ajax request is generated (I am using plain Javascript).

    const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;

    // Create new request add token 
    const generateRequest = new XMLHttpRequest();
    generateRequest.open('POST', '/generate');
    generateRequest.setRequestHeader('X-CSRFToken', csrftoken);

    generateRequest.onload = () => {
        // Get url with the midi file, see below 
    };

    // Add the motif to send with the request
    const data = new FormData();
    data.append('motif', JSON.stringify(notes));

    // Send request

This request sends some notes to the server (Django) that generates a melody.

# Generate the melody based on the seed
@require_http_methods(["POST"])
def generate(request):

# See if method was post
if request.method == "POST":
    # Get the seed and other stuff...

    # Create a melody generator
    mg = MelodyGenerator()

    melody = mg.generate_melody(encoded_seed, ...)

    mg.save_melody(melody)

    # Return the melody 
    return JsonResponse({"melody": melody}, status=200)

The server saves the melody in a midi file that I want to be a temp file

class MelodyGenerator:
# ... do some stuff

    def save_melody(...):
        path_dir = os.path.join(settings.MEDIA_ROOT, "temp_midis")
        file_path = os.path.join(path_dir, file_name)
        midi = stream.write(format, file_path)

So the user can listen the midi (html template):

<midi-player
   src = "url/path/to/temporaryfile"
   sound-font visualizer = "#myVisualizer">
</midi-player>
<midi-visualizer type = "staff" id = "myVisualizer"> </midi-visualizer>

I want this file to be temporary so that it does not take up space, that is, that the user can listen to the melody, have the option to download it, etc, but that when they leave the page, the file is deleted and thus does not take up more space .

How can I know the url of this file, which I want to be temporary?

juancopi81
  • 53
  • 8
  • The server should generate and output the MIDI file on the fly, e.g., `url/path/to/generate.mid?notes=1,2,3` – CL. Sep 17 '20 at 12:49
  • Hi @CL. thanks for the answer... I do not totally understand your explanation, I added more details in the question – juancopi81 Sep 18 '20 at 00:41
  • Instead of returning a JSON response, return the MIDI file itself. – CL. Sep 18 '20 at 05:51
  • Thanks @CL. still not sure how to do it, but I am going to check what you recommend... I would really appreciate any other direction on how to achieve this. My problem is also that I do not know how to create a midi file in music21 without saving it, if I am understanding correctly your comment – juancopi81 Sep 18 '20 at 14:14
  • Instead of `write()`, use [`writeMThdStr()`](http://web.mit.edu/music21/doc/moduleReference/moduleMidi.html#music21.midi.MidiFile.writeMThdStr) and `writestr()`. – CL. Sep 18 '20 at 16:44
  • Thanks @CL. I was able to send the object (in-memory) in an HttpResponse without saving it just by no given any file_path to stream.write("midi") but how I do parse this HttpResponse /create an url out of this midi file, so I can use it in src="" of the midi-player? Is this safe even if there are many users at the same time? – juancopi81 Sep 18 '20 at 20:01
  • I tried 'document.getElementById('myVisualizer').srcObject = generateRequest.response;' but nothing happens – juancopi81 Sep 18 '20 at 20:42
  • Thanks! I solved it, I open the midi file and send it in a HttpResponse, then in Javascript, I created the URL – juancopi81 Sep 19 '20 at 00:44

0 Answers0