Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#97971 - 06/06/2002 12:58 setvol.c
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
Dunno if anyone else has bothered before, but here is a small C program to set the volume level on a RioCar/Empeg player.

This is useful when attempting to play sounds prior to the player having started up, especially when Hijack is installed since Hijack zeros the volume controls at init time.

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

#define MIXER "/dev/mixer"

int main(int argc, char *argv[])
{
int fd;
int vol = -1;

if (argc != 2 || (vol = atoi(argv[1])) < 0 || vol > 100) {
fprintf(stderr, "Usage: %s volume (where 0 <= volume <= 100)\n", argv[0]);
exit(1);
}

fd = open(MIXER, O_WRONLY);
if (fd == -1) {
perror(MIXER);
exit(1);
}

vol |= (vol << 8);
if (ioctl(fd, SOUND_MIXER_WRITE_VOLUME, &vol) == -1) {
perror("SOUND_MIXER_WRITE_VOLUME");
exit(-1);
}
close(fd);
printf("Volume=%d\n", vol & 0xff);
exit(0);
}


Attachments
96458-setvol.gz (92 downloads)


Top
#97972 - 09/06/2002 09:07 Re: setvol.c [Re: mlord]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Very useful, thanks. I am helping someone here shortly to add a startup sound to their unit. I ran into problems when I realised the volume was muted at boot, instead of being at 0db like it was when I last did this.

If anyone is up for enhancing this, my main suggestion is to add a way of setting the volume to the last known value of the car or home volume.

Top
#97973 - 09/06/2002 09:25 Re: setvol.c [Re: drakino]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
By the time you run setvol, the disks are up and spinning, and the savearea has been read in by the kernel.

So.. write a shell script (or modify setvol.c) to read the SaveBase value from /proc/empeg_state, and then seek to that location in /dev/kmem and read the savearea. Then extract the saved vol level from the savearea, taking into account car/home modes, and you're all set. Simple, huh?

-ml

Top
#97974 - 09/06/2002 09:29 Re: setvol.c [Re: mlord]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
Or simpler: do a read() from /dev/empeg_state to retrieve the state. Still gotta get the current power mode (AC/DC) from /proc/empeg_power, and then pull the correct volume setting from the corresponding offset within the savearea.

To figure out the volume location in the savearea, use the Hijack menu item for "Show Flash Savearea" to scroll around while you adjust the volume to see which part of it changes (it gets hilighted). Repeat for the "other" power mode to cover both AC/DC.

This could be a useful app.. go for it!

-ml

Top
#97975 - 09/06/2002 10:20 New improved(!) setvol.c [Re: drakino]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
Okay, this version of setvol (Empeg executable is attached, source is below) now restores the previous "saved" volume level (AC or DC) when run without any arguments. Otherwise, it takes a parameter 0..100 as before.

Enjoy!

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

#define MIXER "/dev/mixer"
#define STATE "/dev/empeg_state"
#define POWER "/proc/empeg_power"

static int Open (const char *path, int mode)
{
int fd = open(path, mode);
if (fd == -1) {
perror(path);
exit(errno);
}
return fd;
}

int main(int argc, char *argv[])
{
int fd;
int vol = -1;

if (argc == 1) {
unsigned char state[128], on_dc;
// restore previous level for this powermode
fd = Open(STATE, O_RDONLY);
if (sizeof(state) != read(fd, state, sizeof(state))) {
perror(STATE);
exit(errno);
}
close(fd);
fd = Open(POWER, O_RDONLY);
if (1 != read(fd, &on_dc, 1)) {
perror(POWER);
exit(errno);
}
close(fd);
on_dc &= 1;
if (on_dc)
vol = (state[0x0d] >> 1);
else
vol = ((state[0x0d] & 1) << 6) | (state[0xc] >> 2);
printf("Saved %s volume was %u\n", on_dc ? "DC" : "AC", vol);
} else if (argc != 2 || (vol = atoi(argv[1])) < 0 || vol > 100) {
fprintf(stderr, "Usage: %s volume (where 0 <= volume <= 100)\n", argv[0]);
exit(-1);
}

fd = Open(MIXER, O_WRONLY);
vol |= (vol << 8);
if (ioctl(fd, SOUND_MIXER_WRITE_VOLUME, &vol) == -1) {
perror("SOUND_MIXER_WRITE_VOLUME");
exit(errno);
}
close(fd);
printf("Volume=%d\n", vol & 0xff);
exit(0);
}


Attachments
96961-setvol (210 downloads)



Edited by mlord (09/06/2002 10:40)

Top
#97976 - 09/06/2002 14:28 Re: New improved(!) setvol.c [Re: mlord]
drakino
carpal tunnel

Registered: 08/06/1999
Posts: 7868
Thanks again. My experience with pure C is still nothing beyond "Hello World" (And my last attempt segfaulted).

Top
#97977 - 09/06/2002 17:12 Re: New improved(!) setvol.c [Re: drakino]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
You're welcome. Just keep in mind that it restores the volume level of the last power-on, not the "most recent" volume level, if different.

Cheers

Top
#97978 - 09/06/2002 20:22 Re: setvol.c [Re: mlord]
mcomb
pooh-bah

Registered: 31/08/1999
Posts: 1649
Loc: San Carlos, CA
I though Mark writing userland stuff was one of the sings of the coming apocalypse? Should I be stocking up on ammo and canned food?

:-)
_________________________
EmpMenuX - ext3 filesystem - Empeg iTunes integration

Top
#97979 - 09/06/2002 20:51 Re: setvol.c [Re: mcomb]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
I though Mark writing userland stuff was one of the sings of the coming apocalypse?

Well, hey, I'm sure even Tiger Woods feels the urge to hit the mini-putt course now and then...
_________________________
- Tony C
my empeg stuff

Top
#97980 - 10/06/2002 06:14 Re: setvol.c [Re: tonyc]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14493
Loc: Canada
Hey, despite all of my now forgotten work on the Linux kernel, most folks who know my name remember it from "hdparm", a simple ioctl() calling userland utility for controlling the IDE driver. So this setvol thingie is right down that alley for me.

Cheers

Top
#97981 - 10/07/2002 09:58 Re: setvol.c - Sound During Bootup [Re: mlord]
Caps
stranger

Registered: 09/07/2002
Posts: 52
Loc: Arizona, USA
Has anybody ever been successful at getting sound to play while the eMpeg is booting up? I was thinking it would be cool to play the THX intro or the other movie sound. There would be a lot of sounds that would work.

I was thinking on how it could be done, and it would have to be very small and accesible very quickly after the power is turned on. A delay of 1-3 seconds would be acceptable, but the sound would have to finish prior to the music starting. This setvol.c code looks promising for getting the volume
turned on, but how would you think I could get the music file to be played very early?

-Thanks


Top
#97982 - 10/07/2002 10:21 Re: setvol.c - Sound During Bootup [Re: Caps]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31597
Loc: Seattle, WA
Yes this has been done. Try searching the BBS on "startup sound".
_________________________
Tony Fabris

Top