Skip to content

Commit

Permalink
use a software volume control over /dev/mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonoughe committed Apr 4, 2009
1 parent 4e9d22a commit 4b3c0ea
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 101 deletions.
10 changes: 2 additions & 8 deletions manual/shell-fm.1
Expand Up @@ -115,16 +115,10 @@ Tag the currently played track/artist/album. Tabulator key completes known
tags.
.TP
.B +
Increase volume (vol).
.TP
.B *
Increase volume (pcm).
Increase volume.
.TP
.B -
Decrease volume (vol).
.TP
.B /
Decrease volume (pcm).
Decrease volume.
.PP
.SH SETUP
Before you start, you should have created the directories
Expand Down
4 changes: 4 additions & 0 deletions source/include/globals.h
Expand Up @@ -20,11 +20,15 @@ extern int batch;
/* Forks. */
extern pid_t playfork, subfork;

extern int playpipe;
extern unsigned char volume;

extern char * currentStation; /* Name of the current station. */

extern unsigned submitting; /* Number of tracks currently submitted. */
extern time_t pausetime; /* Pause start time. */

#define MAX_VOLUME 64

extern unsigned flags;

Expand Down
19 changes: 0 additions & 19 deletions source/include/mix.h

This file was deleted.

2 changes: 1 addition & 1 deletion source/include/play.h
Expand Up @@ -7,6 +7,6 @@

#include <stdio.h>

extern int playback(FILE *);
extern int playback(FILE *, int);

#endif
24 changes: 10 additions & 14 deletions source/interface.c
Expand Up @@ -34,7 +34,6 @@
#include "xmlrpc.h"
#include "recommend.h"
#include "util.h"
#include "mix.h"

#include "globals.h"

Expand Down Expand Up @@ -215,19 +214,17 @@ void interface(int interactive) {
break;

case '+':
adjust(+STEP, VOL);
if(volume < MAX_VOLUME)
volume += 1;
if(playpipe != 0)
write(playpipe, &key, 1);
break;

case '-':
adjust(-STEP, VOL);
break;

case '*':
adjust(+STEP, PCM);
break;

case '/':
adjust(-STEP, PCM);
if(volume > 0)
volume -= 1;
if(playpipe != 0)
write(playpipe, &key, 1);
break;

case 'u':
Expand All @@ -250,9 +247,8 @@ void interface(int interactive) {
"r = change radio station | R = recommend track/artist/album\n"
"S = stop | s = similiar artist\n"
"T = tag track/artist/album | u = show upcoming tracks in playlist\n"
"U = unlove track | + = increase volume (vol)\n"
"- = decrease volume (vol) | * = increase volume (pcm)\n"
"/ = decrease volume (pcm)\n",
"U = unlove track | + = increase volume\n"
"- = decrease volume\n",
stderr
);
break;
Expand Down
56 changes: 0 additions & 56 deletions source/mix.c

This file was deleted.

30 changes: 28 additions & 2 deletions source/play.c
Expand Up @@ -58,6 +58,7 @@ struct stream {

FILE * dump;
char * path;
int pipefd;
};

#define BUFSIZE (32*1024)
Expand All @@ -68,6 +69,8 @@ inline signed scale(mad_fixed_t);

int killed = 0;

unsigned char volume = MAX_VOLUME;

static void sighand(int);

/*
Expand Down Expand Up @@ -103,7 +106,7 @@ mkpath(char *path)
return (0);
}

int playback(FILE * streamfd) {
int playback(FILE * streamfd, int pipefd) {
const char * freetrack = NULL;

killed = 0;
Expand All @@ -129,6 +132,8 @@ int playback(FILE * streamfd) {

data.streamfd = streamfd;
data.parent = getppid();
data.pipefd = pipefd;
fcntl(pipefd, F_SETFL, O_NONBLOCK);

#ifdef LIBAO
data.driver_id = ao_default_driver_id();
Expand Down Expand Up @@ -298,6 +303,25 @@ static enum mad_flow input(void * data, struct mad_stream * stream) {
return MAD_FLOW_CONTINUE;
}

static void read_from_pipe(int pipefd) {
char readchar;
ssize_t readcount;

while((readcount = read(pipefd, &readchar, 1)) > 0) {
switch(readchar) {
// update this process's copy of the volume level
case '+':
if(volume < MAX_VOLUME)
volume += 1;
break;
case '-':
if(volume > 0)
volume -= 1;
break;
}
}
}

#ifdef LIBAO
static enum mad_flow output(
void * data,
Expand Down Expand Up @@ -328,6 +352,7 @@ static enum mad_flow output(
stream_ptr = stream = malloc(pcm->length * (pcm->channels == 2 ? 4 : 2));

assert(stream != NULL);
read_from_pipe(ptr->pipefd);

while(nsample--) {
signed int sample;
Expand Down Expand Up @@ -373,6 +398,7 @@ static enum mad_flow output(
mad_fixed_t * left = pcm->samples[0], * right = pcm->samples[1];

head = NULL;
read_from_pipe(ptr->pipefd);

arg = rate;
ioctl(ptr->audiofd, SOUND_PCM_WRITE_RATE, & arg);
Expand Down Expand Up @@ -413,7 +439,7 @@ inline signed scale(register mad_fixed_t sample) {
else if(sample < -MAD_F_ONE)
sample = -MAD_F_ONE;

return sample >> (MAD_F_FRACBITS + 1 - 16);
return (sample >> (MAD_F_FRACBITS + 1 - 16)) * volume / MAX_VOLUME;
}

static void sighand(int sig) {
Expand Down
12 changes: 11 additions & 1 deletion source/service.c
Expand Up @@ -32,6 +32,7 @@
struct hash data; /* Warning! MUST be bzero'd ASAP or we're all gonna die! */

pid_t playfork = 0; /* PID of the decoding & playing process, if running */
int playpipe = 0;

struct playlist playlist;
char * currentStation = NULL;
Expand Down Expand Up @@ -260,9 +261,13 @@ int play(struct playlist * list) {
for(i = 0; i < (sizeof(keys) / sizeof(char *)); ++i)
set(& track, keys[i], value(& list->track->track, keys[i]));

int pipefd[2];
if(pipe(pipefd) != 0)
return !0;
playfork = fork();

if(!playfork) {
close(pipefd[1]);
FILE * fd = NULL;
const char * location = value(& list->track->track, "location");

Expand All @@ -274,14 +279,19 @@ int play(struct playlist * list) {
fetch(location, & fd, NULL, NULL);

if(fd != NULL) {
if(!playback(fd))
if(!playback(fd, pipefd[0]))
kill(getppid(), SIGUSR2);
close(pipefd[0]);
fshutdown(& fd);
}
}

exit(EXIT_SUCCESS);
}
close(pipefd[0]);
if(playpipe != 0)
close(playpipe);
playpipe = pipefd[1];

return !0;
}

0 comments on commit 4b3c0ea

Please sign in to comment.