Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-11-2006, 04:55 AM   #1
Last Attacker
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Rep: Reputation: 15
A program to rip audio to a RAW file

I have been battling for days/weeks now to try and find out how to extract a track from an audio cd and dump it into a raw file. I did it but its not doing it correctly. Its for a cource project that I'm doing at university. My project is finished but for an extra 10% I have to write a CD dumper.
When I run it through my RAW-audio-file player, all I hear is high pitched garbage sound. I know that my player works because I used a raw file created by cdda2wav and it worked 100%!

So I have a few questions:
* What are the sampling rate, channels, and bits that I can expect when I call the "ioctl(cd, CDROMREADRAW, &msf);" function?
* What mode should I use to read data from a track?
cdrom.h gives me the following (from what I know):
* What should I do to the data read from the CD-Rom to be in the AFMT_S16_BE format? I have tried all the formats to play my raw audio but none works.

Sorry for all the questions but I really can't find anything on the web about this. I hope some of you can help me.

Here is my code for dumping the audio:

struct track_info *tr;
struct cdrom_msf msf;

char buffer[CD_FRAMESIZE_RAW];

int track_index;
int total_frames;	//How many (CD_FRAME_SIZE) frames are going to be needed to read in a whole track.
int i;
int progress_cur;
int progress_total;


//Now we have obtain the necessary information
//about the track we wish to retrieve and work
//out how many FRAMES are going to be needed
//to write out that track to the RAW file.
tr = getTracksInfo();
total_frames = tr[track_index].end_min * CD_SECS * CD_FRAMES + tr[track_index].end_sec * CD_SECS + tr[track_index].end_frame;
total_frames -= tr[track_index].offset_min * CD_SECS * CD_FRAMES + tr[track_index].offset_sec * CD_SECS + tr[track_index].offset_frame;
msf.cdmsf_min0 = tr[track_index].offset_min;
msf.cdmsf_sec0 = tr[track_index].offset_sec;
msf.cdmsf_frame0 = tr[track_index].offset_frame;

progress_total = total_frames / 20;
progress_cur = progress_total;


//Start reading in and writing out frames
for(i = 0; i < total_frames; ++i)
	readData(&msf, buffer, CD_FRAMESIZE_RAW);
	fwrite(buffer, sizeof(char) * CD_FRAMESIZE_RAW, 1, fp);
	if(progress_cur >= progress_total)
		progress_cur = 0;
		printf("%.2f%% complete...\n", i / (float)total_frames * 100.0f);
	if(msf.cdmsf_frame0 >= CD_FRAMES)
		msf.cdmsf_frame0 = 0;
		if(msf.cdmsf_sec0 > CD_SECS)
			msf.cdmsf_sec0 = 0;


void readData(struct cdrom_msf *msf, char *buffer, int frame_size)
	ioctl(cd, CDROMREADRAW, &msf);
	memcpy(buffer, msf, sizeof(char) * frame_size);


struct track_info
	int offset_sec;
	int offset_min;
	int offset_frame;
	int len_sec;
	int len_min;		//You'd be interrested in the len_* fields!
	int len_frame;
	int end_sec;
	int end_min;
	int end_frame;
	BOOL music_track;	//If false, it has to be a data track
Old 04-11-2006, 06:19 PM   #2
Last Attacker
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Original Poster
Rep: Reputation: 15
I came accross this (well I modified it a bit to fit into my app) from some other guy's cdplayer (although his RAW playing also doesn't work):

    printf("Couldn't start device! Provably missing a CD.\n");
    return 1;


s = open("/dev/dsp", O_WRONLY);

if(s == -1)
    printf("Error opening sound device.\n");
    return 1;

    readData(&block_read, CDROMREADAUDIO);
    //fwrite(buffer, 1, CDROMREADAUDIO, fp);
    write(s, buffer, CD_FRAMESIZE_RAW);
    if(block_read.addr.lba == 10000)
(I just added the "if(block_read.addr.lba == 10000)" to the while loop, wasn't part of the code which I got from the other cdplayer)

The strange thing is, it works on my friends pc but not on mine!
Any ideas?
We both have AMD 64-bit PCs with 1GB of ram but the rest of our specs differ. I can tell you what I have but I don't know what he has. Also both of us are running SuSE 10.0

Last edited by Last Attacker; 04-11-2006 at 06:22 PM.
Old 04-12-2006, 10:14 AM   #3
Last Attacker
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Original Poster
Rep: Reputation: 15
For those who are interrested, I got it to work... FINALLY! Thank the Lord!

Take the code above and just add the sampling rate, bits and channels before reading data from the CD. Sampling Rate = 44100, Bits = 16, Channels = 2.
Also don't change the sound format, ie. don't change it from the default Unsigned 8 format.


cd, linux, programming, rip

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Text File to Audio File Program ? rvijay General 5 04-10-2006 03:50 PM
rip the audio from a dvd mohapi Linux - Software 3 08-04-2005 09:14 AM
Can't rip audio cd with Kaudiocreator in Suse 9.1 lennychen SUSE / openSUSE 5 11-02-2004 02:00 AM
Can't get my audio cds to read.. HOW CAN I RIP THEM? theAntic Linux - Newbie 16 07-12-2004 12:38 AM
Rip Audio Track from DVD MikeyCarter Linux - Software 4 07-24-2003 11:59 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration