med

music editor
git clone git://git.kocotian.pl/cbms.git
Log | Files | Refs

commit 5917fcb2c61ab233041f014b49accc863af7a6c7
parent 1f4bcd83590d554b6b2c3e4f85cd69baeb3d3bc8
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun,  6 Dec 2020 15:19:37 +0100

prototype wave playing, strrep, additions

Diffstat:
Mlicenses/LIBSL | 1+
Mmed | 0
Mmed.c | 43++++++++++++++++++++++++++++++++++---------
Mutil.c | 8++++++++
Mutil.h | 1+
5 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/licenses/LIBSL b/licenses/LIBSL @@ -4,6 +4,7 @@ MIT/X Consortium License © 2015-2019 Hiltjo Posthuma <hiltjo@codemadness.org> © 2016 Markus Teich <markus.teich@stusta.mhn.de> © 2015 Eric Pruitt <eric.pruitt@gmail.com> +© 2020 Kacper Kocot <kocotian@kocotian.pl> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), diff --git a/med b/med Binary files differ. diff --git a/med.c b/med.c @@ -17,10 +17,11 @@ typedef struct { char modificated; } Wave; -static void changewavsel(Wave *wave, char isRight, char *l); +static void changewavselection(Wave *wave, char isRight, char *l); static void docommand(Wave **waves, size_t *waven, int *selwav, char *l); static void editwave(Wave **waves, size_t *waven, char *wname); static void newwave(Wave **waves, size_t *waven, char *wname); +static void playwave(Wave wave); static void printwaveinfo(Wave wave); static void printwavelist(Wave *waves, size_t waven); static Wave readf32(char *filename, char endianness, int sampleRate, int channels); @@ -37,7 +38,7 @@ static void usage(void); char *argv0; static void -changewavsel(Wave *wave, char isRight, char *l) +changewavselection(Wave *wave, char isRight, char *l) { size_t *val = isRight ? &(wave->rightSelection) : &(wave->leftSelection); char secs = l[strlen(l) - 1] == 's' ? 1 : 0; @@ -98,9 +99,30 @@ newwave(Wave **waves, size_t *waven, char *wname) strncpy((*waves)[(*waven) - 1].name, wname, strlen(wname)); (*waves)[(*waven) - 1].sampleRate = 48000; (*waves)[(*waven) - 1].channels = 2; - (*waves)[(*waven) - 1].leftSelection = - (*waves)[(*waven) - 1].rightSelection = -1; - (*waves)[(*waven) - 1].modificated = 0; +} + +static void +playwave(Wave wave) +{ + char *wname, *cmd; + wname = calloc(strlen(wave.name) + 6 /* "/tmp/" = 5, plus null + terminator = 6 */, 0); + cmd = calloc(BUFSIZ, 0); + free(cmd); /* this is really stupid, i don't know how it works, + without calloc and freeing memory, with NULL as value + of cmd, it crashes. After calloc and freeing allocated + memory it works, when it should not. TODO */ + strcat(wname, "/tmp/"); + strcat(wname, wave.name); + strrep(wname + 5, '/', '_'); + savef32(wname, wave, 0); + snprintf(cmd, BUFSIZ, "ffplay -autoexit -f f32le -ar %d -channels %d -showmode 0 %s 2> /dev/null", + wave.sampleRate, wave.channels, wname); + printf("playing: wave \"%s\" @ %dHz with %d channels\n", + wave.name, wave.sampleRate, wave.channels); + system(cmd); + unlink(wname); + free(wname); } static void @@ -142,7 +164,7 @@ printwavelist(Wave *waves, size_t waven) waves - ws, (*waves).name); } -static Wave +static Wave /* 1 is bigger than 0, so 1 is big endian ;) */ readf32(char *filename, char endianness, int sampleRate, int channels) { FILE *fp = NULL; /* wave *file */ @@ -160,9 +182,10 @@ readf32(char *filename, char endianness, int sampleRate, int channels) ret.name = filename; ret.wave = malloc(0); - ret.wsize = 0; + ret.wsize = ret.modificated = 0; ret.sampleRate = sampleRate ? sampleRate : 48000; ret.channels = channels ? channels : 2; + ret.leftSelection = ret.rightSelection = -1; while ((ch[endianness ? 3 : 0] = fgetc(fp), /* this code will work only on | ? 0 : 3] = | */ ch[endianness ? 2 : 1] = fgetc(fp), /* little endian architectures | ? 1 : 2] = | */ @@ -237,6 +260,8 @@ shell(Wave **waves, size_t *waven) case 'n': /* new */ newwave(waves, waven, *(l + 1) == ' ' ? l + 2 : l + 1); break; + case 'p': /* play wave */ + playwave((*waves)[selwav]); break; case 's': /* select wave */ selectwave(*waves, *waven, &selwav, l); break; case 'w': /* write */ @@ -245,9 +270,9 @@ shell(Wave **waves, size_t *waven) case 'q': /* quit */ goto stop; break; case 'L': /* left selection change */ - changewavsel(&((*waves)[selwav]), 0, l + 1); break; + changewavselection(&((*waves)[selwav]), 0, l + 1); break; case 'R': /* left selection change */ - changewavsel(&((*waves)[selwav]), 1, l + 1); break; + changewavselection(&((*waves)[selwav]), 1, l + 1); break; default: puts("?"); break; } diff --git a/util.c b/util.c @@ -33,3 +33,11 @@ die(const char *fmt, ...) { exit(1); } + +void +strrep(char *str, char src, char dest) +{ + --str; + while (*(++str)) + if (*str == src) *str = dest; +} diff --git a/util.h b/util.h @@ -6,3 +6,4 @@ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); +void strrep(char *str, char src, char dest);