Mailing List Archive

[PATCH] mono support
So far I haven't been able to get my cheap AverTV Stereo card to
actually work in stereo, so I needed this patch to kill the chipmunks.
The audio is still out of sync though. Isaac, can you think of any
other places that need to be updated? I don't understand how the
audio is synced yet.

-Jim

Index: libs/libNuppelVideo/NuppelVideoPlayer.cpp
===================================================================
RCS file: /var/lib/cvs/MC/libs/libNuppelVideo/NuppelVideoPlayer.cpp,v
retrieving revision 1.95
diff -u -r1.95 NuppelVideoPlayer.cpp
--- libs/libNuppelVideo/NuppelVideoPlayer.cpp 17 Nov 2002 11:51:26 -0000 1.95
+++ libs/libNuppelVideo/NuppelVideoPlayer.cpp 17 Nov 2002 23:30:23 -0000
@@ -75,6 +75,7 @@
weMadeBuffer = false;

osd = NULL;
+ audio_channels = 2;
audio_samplerate = 44100;
editmode = false;
advancevideo = resetvideo = advancedecoder = false;
@@ -171,12 +172,12 @@

void NuppelVideoPlayer::InitSound(void)
{
- int bits = 16, stereo = 1, speed = audio_samplerate, caps;
+ int bits = 16, channels = audio_channels, speed = audio_samplerate, caps;

if (usingextradata)
{
bits = extradata.audio_bits_per_sample;
- stereo = (extradata.audio_channels == 2);
+ channels = extradata.audio_channels;
speed = extradata.audio_sample_rate;
}

@@ -202,11 +203,18 @@
return;
}

- if (ioctl(audiofd, SNDCTL_DSP_STEREO, &stereo) < 0)
+ cout << "setting channels to " << channels << endl;
+ if (ioctl(audiofd, SNDCTL_DSP_CHANNELS, &channels) < 0)
{
cerr << "problem setting to stereo, exiting\n";
close(audiofd);
audiofd = -1;
return;
}
+ cout << "actually set channels to " << channels << endl;
+ if(channels != audio_channels) {
+ cout << "output forced to use " << channels
+ << " instead of " << audio_channels << endl;
+ audio_channels = channels;
+ }

@@ -479,8 +488,10 @@

foundit = 0;
effdsp = audio_samplerate;
- if (usingextradata)
+ if (usingextradata) {
effdsp = extradata.audio_sample_rate;
+ audio_channels = extradata.audio_channels;
+ }

while (!foundit)
{
@@ -856,7 +867,7 @@
ioctl(audiofd, SNDCTL_DSP_GETODELAY, &soundcard_buffer); // bytes
totalbuffer = audiolen(false) + soundcard_buffer;

- audiotime = audbuf_timecode - (int)((double)totalbuffer * 25000.0 /
+ audiotime = audbuf_timecode - (int)((double)totalbuffer * 50000.0 / audio_channels /
(double)effdsp);

gettimeofday(&audiotime_updated, NULL);
Index: libs/libNuppelVideo/NuppelVideoPlayer.h
===================================================================
RCS file: /var/lib/cvs/MC/libs/libNuppelVideo/NuppelVideoPlayer.h,v
retrieving revision 1.48
diff -u -r1.48 NuppelVideoPlayer.h
--- libs/libNuppelVideo/NuppelVideoPlayer.h 15 Nov 2002 00:12:07 -0000 1.48
+++ libs/libNuppelVideo/NuppelVideoPlayer.h 17 Nov 2002 23:30:23 -0000
@@ -183,6 +183,7 @@
unsigned char *buf2;
char lastct;
int effdsp; // from the recorded stream
+ int audio_channels; // from the recorded stream
int audio_samplerate; // rate to tell the output device
int filesize;
int startpos;
Index: libs/libNuppelVideo/NuppelVideoRecorder.cpp
===================================================================
RCS file: /var/lib/cvs/MC/libs/libNuppelVideo/NuppelVideoRecorder.cpp,v
retrieving revision 1.61
diff -u -r1.61 NuppelVideoRecorder.cpp
--- libs/libNuppelVideo/NuppelVideoRecorder.cpp 16 Nov 2002 20:08:08 -0000 1.61
+++ libs/libNuppelVideo/NuppelVideoRecorder.cpp 17 Nov 2002 23:30:25 -0000
@@ -71,6 +71,7 @@
keyframedist = KEYFRAMEDIST;

audiobytes = 0;
+ audio_channels = 2;
audio_samplerate = 44100;

picture_format = PIX_FMT_YUV420P;
@@ -331,11 +333,19 @@
if (afmt != AFMT_S16_LE)
{
cerr << "Can't get 16 bit DSP, exiting\n";
- return(1);
+ return 1;
}

- channels = 2;
- ioctl(afd, SNDCTL_DSP_CHANNELS, &channels);
+ channels = audio_channels;
+ if(ioctl(afd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
+ cerr << "setting sample channels failed, exiting\n";
+ return 1;
+ }
+ if(channels != audio_channels) {
+ cout << "input forced to use " << channels
+ << " instead of " << audio_channels << endl;
+ audio_channels = channels;
+ }

/* sample rate */
rate = audio_samplerate;
@@ -354,7 +385,7 @@
if (-1 == ioctl(afd, SNDCTL_DSP_GETBLKSIZE, &blocksize))
{
cerr << "Can't get DSP blocksize, exiting\n";
- return(1);
+ return 1;
}
blocksize *= 4;

@@ -1107,7 +1138,7 @@
}

moredata.audio_sample_rate = audio_samplerate;
- moredata.audio_channels = 2;
+ moredata.audio_channels = audio_channels;
moredata.audio_bits_per_sample = 16;

extendeddataOffset = ringBuffer->GetFileWritePosition();
@@ -1264,12 +1295,25 @@
return;
}

- channels = 2;
- ioctl(afd, SNDCTL_DSP_CHANNELS, &channels);
+ channels = audio_channels;
+ if(ioctl(afd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
+ cerr << "Can't set DSP channels, exiting";
+ close(afd);
+ return;
+ }
+ if(channels != audio_channels) {
+ cout << "input forced to use " << channels
+ << " instead of " << audio_channels << endl;
+ audio_channels = channels;
+ }

/* sample rate */
rate = audio_samplerate;
- ioctl(afd, SNDCTL_DSP_SPEED, &rate);
+ if(ioctl(afd, SNDCTL_DSP_SPEED, &rate) < 0) {
+ cerr << "Can't set DSP sample rate, exiting";
+ close(afd);
+ return;
+ }

if (-1 == ioctl(afd, SNDCTL_DSP_GETBLKSIZE, &blocksize))
{
Index: libs/libNuppelVideo/NuppelVideoRecorder.h
===================================================================
RCS file: /var/lib/cvs/MC/libs/libNuppelVideo/NuppelVideoRecorder.h,v
retrieving revision 1.24
diff -u -r1.24 NuppelVideoRecorder.h
--- libs/libNuppelVideo/NuppelVideoRecorder.h 15 Nov 2002 21:40:21 -0000 1.24
+++ libs/libNuppelVideo/NuppelVideoRecorder.h 17 Nov 2002 23:30:25 -0000
@@ -126,6 +126,7 @@
int compression;
int compressaudio;
unsigned long long audiobytes;
+ int audio_channels; // channels to request from sounddevice
int audio_samplerate; // rate we request from sounddevice
int effectivedsp; // actual measured rate
Re: [PATCH] mono support [ In reply to ]
On Wednesday 13 November 2002 08:33 pm, Jim Radford wrote:
> So far I haven't been able to get my cheap AverTV Stereo card to
> actually work in stereo, so I needed this patch to kill the chipmunks.
> The audio is still out of sync though. Isaac, can you think of any
> other places that need to be updated? I don't understand how the
> audio is synced yet.

Yeah -- all the calculations in the audio sync code assume that the audio is
stereo. You'd need to fix those assumptions as well. Bunch of magic numbers
in the player code.. It might be easier just to open the device as mono as
you are currently, then transform the audio buffer to stereo real quick after
reading the data from the sound card, or even just use lame_encode_buffer()
instead of lame_encode_buffer_interleaved() and duplicate the mono channel to
stereo that way.

Isaac
Re: [PATCH] mono support [ In reply to ]
Jim, I guess I never saw your email. I also have an AverTV
Stereo card which i bought from CompUSA. The problem with
the card is that the bttv driver doesn't know the card. I
even downloaded the latest version from
http://www.bytesex.org/bttv and no success on autodetection.
When I look at dmesg the card gets autodetected as card 13
and everything works except for the audio being the SAP
channel. I didn't find any solutions anywhere but finally
figured out that the audiomux settings for the autodetected
card were to blame.

Using bttools in Windows with the drivers installed I
figured out that the correct setting to get stereo sound is
0xff44e. So:

modprobe bttv audiomux=0xff44e

For some reason it didn't seem to work from modules.autoload
so I had to manually rmmod and modprobe every time. That
gave me problems when I got the second card though because
when you modprobe bttv it detects both cards and the other
card didn't like the audiomux setting.

My solution was to go to the bttv source (bttv-cards.c),
change the first value in the audiomux array to 0xff44e,
make, make install and modprobe bttv. Now everything works
just fine without having to put any arguments on bttv.

-Emil

On Mon, 18 Nov 2002 06:13:27 -0800 (PST), Isaac Richards
wrote:

>
> On Wednesday 13 November 2002 08:33 pm, Jim Radford wrote:
> > So far I haven't been able to get my cheap AverTV Stereo
> card to
> > actually work in stereo, so I needed this patch to kill
> the chipmunks.
> > The audio is still out of sync though. Isaac, can you
> think of any
> > other places that need to be updated? I don't understand
> how the
> > audio is synced yet.
>
> Yeah -- all the calculations in the audio sync code assume
> that the audio is
> stereo. You'd need to fix those assumptions as well.
Bunch
> of magic numbers
> in the player code.. It might be easier just to open the
> device as mono as
> you are currently, then transform the audio buffer to
stereo
> real quick after
> reading the data from the sound card, or even just use
> lame_encode_buffer()
> instead of lame_encode_buffer_interleaved() and duplicate
> the mono channel to
> stereo that way.
>
> Isaac
> _______________________________________________
> mythtv-dev mailing list
> mythtv-dev@snowman.net
> <a
href="http://mail.telocity.com/jump/http://www.snowman.net/mailman/listinfo/mythtv-dev">http://www.snowman.net/mailman/listinfo/mythtv-dev</a>
Re: [PATCH] mono support [ In reply to ]
Jim,

It would be great to parameterize all of the player/recorder code in
terms of 'channels' and 'bytes_per_sample' etc. The option to use mono
would be pretty sweet for people with mono tuners, especially if they
don't have the world's fastest CPU.

I made some changes to the a/v sync code over the weekend, which are in
CVS now. The patch you posted will collide with them. Sorry about that.
It shouldn't be too hard to straighten out...

There are two changes now in cvs:

* A/V sync will account for audio sitting in the sound card's capture
buffer. I forgot this before, and it was introducing some error. Whereas
now there is absolutely zero error. ;)
* timecodes are referenced to the end of video frames, and to the end
of audio chunks, rather than the beginning. This made the code simpler
in 3 places and more complicated in 1 place.

( By the way -- if you're going to work on the player -- take a minute
to see if you can figure out how the audio buffer's read pointer could
"lap" the write pointer. This should never happen, though Isaac seems to
have observed just this. We were stymied, the code looked pretty solid,
to me anyway...)

thanks!

- john