DSPRelated.com
Forums

Beginner Questions

Started by chuckles May 23, 2005
Hullo all,
     I have a few more questions, sorry if the seem very basic but I am
having some trouble finding answers. First of all I am programming in
linux, c++. My app is reading from /dev/dsp. I have developed a nice
base for my program, I can store, playback/record samples, change
volume levels etc. I want to implement some digital effects and am
focusing on distortion/compression atm. My questions are as follows.

1. When i read info from /dev/dsp and store as an array of floats, how
should i interpret the data? What information is being represented? For
an instance I have read some examples of compression which suggested
(as a first step) implementing a low-pass filter with a cutoff
frequency of 15-30hz. I have seen simple examples of low pass filters
blurring some text in an image but don't know how I go about
implementing one for audio or in c++. Are there any programming
tutorials out there? As a cut off frequency is suggested i presume my
input array should be ffted to pull the frequencies out? Also how can i
convert my input array to decibels? I probably won't need it but it
might be nice to know.

2. C ioctl specific question. I have been reading the helpful audio
guide at the OSS home page. One of the suggestions given in the
tutorial was to  use ioctl(fd, SOUND_PCM_RESET, 0) where fd = /dev/dsp,
to stop recording abruptly. This works once but any attempt at
recording after this call causes my program to fail while reading
/dev/dsp with an I/O error.

3. I found a number of algorithms spelt out in pseudo code at <a href =
http://users.iafrica.com/k/ku/kurient/dsp/algorithms.html> this
site</a>

I was trying to implement the distortion effect described below.

"Compression, at it's simplest, uses a logarithmic transfer function:

 int in;

 for (i=0; i <= MaxSamples; i++)

 { in=unprocessed_buf[i];

   processed_buf[i]=32767*sign(in)*(abs(in)/32767)^(1-level/10)

 }

The range of level is 0 to 10 and the samples are 16-bit words. The
higher the level, the greater the distortion. Thus, you can achieve
distortion this way. "


I can't get it to work, I'm a little confused as to the format of
unprocessed_buf int or float? And also the meaning of ^ in the
algorithm is it the pow or xor function?

As always if anyone can shed light on any of these matters that'd be
great. Thanks for you help so far, keep it up^^

Chuckles

chuckles wrote:
> > Hullo all, > I have a few more questions, sorry if the seem very basic but I am > having some trouble finding answers. First of all I am programming in > linux, c++. My app is reading from /dev/dsp.
Be aware that the /dev/dsp device provided by the OSS drivers is deprecated in favour of ALSA. At some time in future, the OSS drivers will disappear.
> I have developed a nice > base for my program, I can store, playback/record samples, change > volume levels etc. I want to implement some digital effects and am > focusing on distortion/compression atm. My questions are as follows. > > 1. When i read info from /dev/dsp and store as an array of floats, how > should i interpret the data? What information is being represented?
Most of this is explained (briefly) here: http://www.mega-nerd.com/Res/IADSPL/page00.html
> 2. C ioctl specific question. I have been reading the helpful audio > guide at the OSS home page. One of the suggestions given in the > tutorial was to use ioctl(fd, SOUND_PCM_RESET, 0) where fd = /dev/dsp, > to stop recording abruptly. This works once but any attempt at > recording after this call causes my program to fail while reading > /dev/dsp with an I/O error.
I think you need to do: ioctl (fd, SNDCTL_DSP_SYNC, 0) before you try and read again.
> 3. I found a number of algorithms spelt out in pseudo code at <a href = > http://users.iafrica.com/k/ku/kurient/dsp/algorithms.html> this > site</a> > > I was trying to implement the distortion effect described below. > > "Compression, at it's simplest, uses a logarithmic transfer function: > > int in; > > for (i=0; i <= MaxSamples; i++) > > { in=unprocessed_buf[i]; > > processed_buf[i]=32767*sign(in)*(abs(in)/32767)^(1-level/10) > > }
In the audio context, compression usually means "dynamic range compression". What you posted is soft-clipping. For a compressor, have a look in this code: http://www.mega-nerd.com/Res/IADSPL/IADSPL-Code-1.tar.gz This code is over 3 years old now and I'm not certain if it still compiles.
> I can't get it to work, I'm a little confused as to the format of > unprocessed_buf int or float?
It looks like the code above assumes ints.
> And also the meaning of ^ in the > algorithm is it the pow or xor function?
That looks like C where caret is the XOR function. Erik -- +-----------------------------------------------------------+ Erik de Castro Lopo nospam@mega-nerd.com (Yes it's valid) +-----------------------------------------------------------+ "TLC declared bankruptcy after they received less than 2 percent of the $175 million earned by their CD sales. That was about 40 times less than the profit that was divided among their management, production and record companies." -- Courtney Love on the REAL piracy
Hello Eric,
    Thanks for your reply, your tutorial is very clear. I have a
question regarding the sound volume section:

"The difference in volume between the signal being discussed and some
reference is usually measured in decibels. Given a reference volume r
and a signal volume x, the difference in volume is given by "

How do you measure the volume of noise if you were attempting to find
your SNR? As a test I have been using the following method:

void MixDesk::toDecibel(int x){
  working = &(memory[x]);
  float *orig = working -> getSample();


  for(int i = 0; i < working->getLength(); i++){
    cout<<20*(log10(orig[i])/5)<<endl;
  }
}

Above I set r to 5 and used single values from the sample to set x. Is
this correct? How do i set these values to provide meaningful results?

The next question is not relevant to comp.dsp but I thought someone may
be able to lend some insight.

 I tried upgrading to alsa but was unable to find a kernel-module-alsa
rpm to fit my distro.  (RH9 2.4.20-8). This seemed a bit odd as that's
the kernel that comes as standard with RH. I had altered my
modules.conf file before I discovered I was unable to find the
kernel-module rpm. I would like to avoid compiling the kernel so I
decided to reset my modules.conf file and use oss regardless.  RH
booted up fine and I am able to play cds but whenever I play an mp3
file(xmms etc.) or record sound from line in and then playback I get
very choppy output. I have a suspicion it mayb a frag size problem so I
ran sndconfig and the redhat soundcard config app. Both detected and
installed a new modules.conf file but the choppy mp3s remain. One thing
I have noticed is the sound modules are not loaded on boot (when i type
lsmod they don't appear) but the modules are auto loaded when I select
a sound app.

My modules.conf file is written by sndconfig so I think its ok. Does
anyone have any ideas as to the root of this problem? It never occurred
before I tried to install alsa.

As always thanks for any and all help.

Cathal