feat(midi): export selected clip as an atomic Standard MIDI File (phase 103)

This commit is contained in:
Johannes Findeisen 2026-11-06 02:26:20 +01:00
commit 02ee1818a3
9 changed files with 178 additions and 5 deletions

60
src/midi/file.c Normal file
View file

@ -0,0 +1,60 @@
#define _POSIX_C_SOURCE 200809L
#include "xtc/xtc_midi_file.h"
#include "../project/project_internal.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#define SMF_EXPORT_BYTES 65536U
typedef struct {uint64_t tick,id;unsigned char status,pitch,velocity;} smf_event;
static void be32(unsigned char *p,uint32_t v){p[0]=(unsigned char)(v>>24);p[1]=(unsigned char)(v>>16);p[2]=(unsigned char)(v>>8);p[3]=(unsigned char)v;}
static void vlq(unsigned char *data,size_t *size,uint64_t value)
{
unsigned char bytes[4];size_t n=0;
bytes[n++]=(unsigned char)(value&127);while((value>>=7))bytes[n++]=(unsigned char)((value&127)|128);
while(n)data[(*size)++]=bytes[--n];
}
static int event_compare(const void *a,const void *b)
{
const smf_event *x=a,*y=b;
if(x->tick!=y->tick)return x->tick<y->tick ? -1 : 1;
if((x->status&0xf0)!=(y->status&0xf0))return (x->status&0xf0)==0x80 ? -1 : 1;
return x->id<y->id ? -1 : x->id!=y->id;
}
xtc_result xtc_project_export_midi_clip(const xtc_project *p,uint64_t id,const char *path)
{
xtc_clip_info c;smf_event *events=NULL;unsigned char *data=NULL;size_t i,count=0,size=22;uint64_t previous=0;
char temporary[4096];int fd,n;FILE *f;xtc_result r;uint32_t tempo;
if(!p || !path || !*path)return XTC_ERR_INVALID_ARGUMENT;
r=xtc_project_find_clip(p,id,&c);if(r!=XTC_OK)return r;if(!c.midi)return XTC_ERR_INVALID_STATE;
events=calloc(XTC_PROJECT_MAX_NOTES*2,sizeof(*events));data=calloc(SMF_EXPORT_BYTES,1);
if(!events || !data){free(events);free(data);return XTC_ERR_OUT_OF_MEMORY;}
for(i=0;i<p->note_count;++i)if(p->notes[i].clip_id==id){
const xtc_note_info *note=p->notes+i;uint64_t start=note->start_tick,end=start+note->length_tick;
if(start<c.offset_tick)start=c.offset_tick;
if(end>c.offset_tick+c.length_tick)end=c.offset_tick+c.length_tick;
if(start>=end)continue;
events[count++]=(smf_event){start-c.offset_tick,note->id,(unsigned char)(0x90|note->channel),(unsigned char)note->pitch,(unsigned char)note->velocity};
events[count++]=(smf_event){end-c.offset_tick,note->id,(unsigned char)(0x80|note->channel),(unsigned char)note->pitch,0};
}
qsort(events,count,sizeof(*events),event_compare);
memcpy(data,"MThd",4);be32(data+4,6);data[11]=1;data[12]=3;data[13]=0xc0;memcpy(data+14,"MTrk",4);
data[size++]=0;data[size++]=0xff;data[size++]=3;vlq(data,&size,strlen(c.name));memcpy(data+size,c.name,strlen(c.name));size+=strlen(c.name);
tempo=(uint32_t)((UINT64_C(60000000000)+p->tempo/2)/p->tempo);
data[size++]=0;data[size++]=0xff;data[size++]=0x51;data[size++]=3;
data[size++]=(unsigned char)(tempo>>16);data[size++]=(unsigned char)(tempo>>8);data[size++]=(unsigned char)tempo;
for(i=0;i<count;++i){vlq(data,&size,events[i].tick-previous);previous=events[i].tick;data[size++]=events[i].status;data[size++]=events[i].pitch;data[size++]=events[i].velocity;}
vlq(data,&size,c.length_tick-previous);data[size++]=0xff;data[size++]=0x2f;data[size++]=0;
be32(data+18,(uint32_t)(size-22));free(events);
n=snprintf(temporary,sizeof(temporary),"%s.tmp-XXXXXX",path);
if(n<0 || (size_t)n>=sizeof(temporary)){free(data);return XTC_ERR_CAPACITY;}
fd=mkstemp(temporary);if(fd<0){free(data);return XTC_ERR_IO;}
f=fdopen(fd,"wb");if(!f){close(fd);unlink(temporary);free(data);return XTC_ERR_IO;}
r=fwrite(data,1,size,f)==size ? XTC_OK : XTC_ERR_IO;free(data);
if(fflush(f)!=0 || fsync(fd)!=0)r=XTC_ERR_IO;
if(fclose(f)!=0)r=XTC_ERR_IO;
if(r==XTC_OK && rename(temporary,path)!=0)r=XTC_ERR_IO;
if(r!=XTC_OK)unlink(temporary);
return r;
}