User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 428,381 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,608 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 856 | Replies: 4 | Solved
Reply
Join Date: Jun 2008
Posts: 3
Reputation: real_castilla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
real_castilla real_castilla is offline Offline
Newbie Poster

Read a raw PCM file and then play it (with sound) in C or C++

  #1  
Jun 8th, 2008
Hi all,
I'm a newbie with C or C++. I have a raw PCM file 16-bit (like a wav file but no header). I need to build a console application that can read the file, store data in an array, modify it (ex: add 100 to each value of the data array), then play it in sound (with a known frequency sampling, ex Fs = 16000 Hz).
It would be very nice if someone can explain to me how to do that.
Thank you very much.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,647
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Read a raw PCM file and then play it (with sound) in C or C++

  #2  
Jun 9th, 2008
Sure, which bit exactly are you having problems with?

- reading into an array
- adding values
- playing it
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: real_castilla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
real_castilla real_castilla is offline Offline
Newbie Poster

Re: Read a raw PCM file and then play it (with sound) in C or C++

  #3  
Jun 9th, 2008
Originally Posted by Salem View Post
Sure, which bit exactly are you having problems with?

- reading into an array
- adding values
- playing it



Thks for your answer
Here is my code of reading values into an array
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7. std::fstream f_in;
  8. short speech, value[10000];
  9.  
  10.  
  11. f_in.open ("myfile.pcm", std::ios::in | std::ios::binary);
  12.  
  13.  
  14. int i = 0;
  15. while (i < 10000) {
  16. f_in.read((char *)&speech, 2);
  17. value[i] = speech;
  18. cout << speech << std::endl;
  19. i++;
  20. }
  21. }
I think it works well.
The problem is how to play sound from value array, with frequency sampling Fs = 16000hz.
Thks for any advice
Last edited by Ancient Dragon : Jun 9th, 2008 at 6:16 pm. Reason: add code tags
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,229
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 937
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Read a raw PCM file and then play it (with sound) in C or C++

  #4  
Jun 10th, 2008
I can't help you with your problem, but you don't need that while loop
f_in.read(speech, 10000 * sizeof(short));
But of course there is no guarentee that sizeof(short) will be 2. It might be on your system but if you intend to port that porgram to another os then the size may be something else.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: real_castilla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
real_castilla real_castilla is offline Offline
Newbie Poster

Re: Read a raw PCM file and then play it (with sound) in C or C++

  #5  
Jun 11th, 2008
Thks for all of you.
I found my answer here:
http://www.insomniavisions.com/docum...rials/wave.php
Thks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 7:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC