• Domingo 28 de Abril de 2024, 23:35

Autor Tema:  Como Leer Y Escribir Archivos De Formato Wav,mp3.  (Leído 1686 veces)

ricardo8204

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Como Leer Y Escribir Archivos De Formato Wav,mp3.
« en: Viernes 3 de Septiembre de 2004, 23:50 »
0
:unsure:  :unsure: por favor si alguien sabe como leer y escribir (NO REPRODUCIR CON EL REPRODUCTOR DE WINDOWS) archivos en formatos WAV, Mp3. por favor hacermelo saber es que lo necesito con urgencia.

gracias

Amilius

  • Miembro HIPER activo
  • ****
  • Mensajes: 665
    • Ver Perfil
Re: Como Leer Y Escribir Archivos De Formato Wav,mp3.
« Respuesta #1 en: Viernes 22 de Octubre de 2004, 18:54 »
0
Veo que no sólo necesitas orientación en un tema puntual, por lo que te recomiento buscar componentes open source en "www.torry.net" o en "sourceforge.net".

Adjunto una biblioteca para leer archivos .WAV PCM que elaboré para ser usada para cargar los buffers de sonido de DirectSound. Es ideal si no quieren que su ejecutable "engorde" usando componentes que además escriben y reproducen archivos .WAV y romperse la cabeza buscando como usarlos para crear y llenar los buffers de directsound.

Código: Text
  1.  
  2. unit LectorWAV;
  3. (*
  4. Autor: Ing. Sergio A. Chávez R.
  5.  
  6. La clase TlectorWav permite leer un archivo .WAV PCM en un buffer para directSound.
  7. Pasos para usarlo:
  8.  1.- Crear el objeto TlectorWav indicando el nombre del archivo ".WAV".
  9.  2.- Verificar la propiedad ".preparado" de TlectorWav, falso=>cancelar todo, error al leer el archivo.
  10.  3.- Preparar el descriptor de buffer para directsound usando ".TamannoDatos" y ".formatoOnda" de TlectorWav;
  11.  4.- Crear el buffer de sonido directsound con el descriptor de buffer preparado.
  12.  5.- Usar el procedimiento "lock" del buffer de sonido directsound creado para obtener la referencia al buffer y su tamaño.
  13.  6.- Llamar al método ".Leer" de TlectorWav indicando la posición inicial del buffer y el tamaño del buffer.
  14. *)
  15.  
  16. interface
  17.  
  18. uses mmsystem;//Para el TWaveFormatEx, para usar con directSound.
  19.  
  20. type
  21.   TWavTag=array[0..3] of char;
  22.   TErrorWavSound=integer;
  23.  
  24. const
  25.   //Errores:
  26.   WV_Ok=0;
  27.   WV_EncabezadoIncorrecto=1;
  28.   //Constantes auxiliares
  29.   MaxTammanoBuffer=32768;
  30.   smTelefono:integer=11025;
  31.   smRadio:integer=22050;
  32.   smCD:integer=44100;
  33.   chMono:word=1;
  34.   chStereo:word=2;
  35.   rsRadio:word=8;
  36.   rsCD:word=16;
  37.   Inicio_Area_Datos=44;
  38.   tgRIFF:TWavTag=('R','I','F','F');
  39.   tgWAVE:TWavTag=('W','A','V','E');
  40.   tgfmt:TWavTag=('f','m','t',' ');
  41.   tgdata:TWavTag=('d','a','t','a');
  42. (*
  43. Control de tamaño de archivo. Es totalmente opcional pero es recomendable no
  44. crear buffers demasiado grandes fraccionandolos en varios de menor tamaño.
  45. *)
  46.   MaximoAceptable=1048576;//Máximo archivos de 1MB
  47.   //Tags Para formato PCM
  48.   PCMversion1:integer=$10;
  49.   PCMversion2:word=$1;
  50.  
  51. type
  52.   TlectorWav=class(TObject)
  53.   private
  54.     Tamanno_Datos:integer;
  55.     formato:TWaveFormatEx;
  56.     fabierto:bytebool;
  57.     archivo:file;
  58.     destructor destroy; override;
  59.   public
  60.     property TamannoDatos:Integer read Tamanno_Datos;
  61.     property BitsPorMuestra:word read formato.wbitspersample;
  62.     property Canales:word read formato.nchannels;
  63.     property Muestras:integer read formato.nSamplesPerSec;
  64.     property FormatoOnda:TWaveFormatEx read formato;
  65.     property Preparado:bytebool read fabierto;
  66.     constructor create(const FileName:string);
  67.     function Leer(var Buffer; Longitud:integer):boolean;
  68.   end;
  69.  
  70. implementation
  71.  
  72. constructor TlectorWav.create(const FileName:string);
  73.   function LeerEncabezado:TErrorWavSound;
  74.   var tag:TWavTag;
  75.       ver1,Bytes_Por_Segundo,Tamanno_Total:integer;
  76.       ver2,Bytes_Por_Muestra:word;
  77.   begin
  78.     result:=WV_EncabezadoIncorrecto;
  79.     BlockRead(Archivo,tag,4);
  80.     if tag<>tgRIFF then exit;
  81.     BlockRead(Archivo,Tamanno_Total,4);
  82.     if Tamanno_Total>MaximoAceptable then exit;
  83.     BlockRead(Archivo,tag,4);
  84.     if tag<>tgWAVe then exit;
  85.     BlockRead(Archivo,tag,4);
  86.     if tag<>tgfmt then exit;
  87.     BlockRead(Archivo,ver1,4);
  88.     if ver1<>PCMversion1 then exit;
  89.     BlockRead(Archivo,ver2,2);
  90.     if ver2<>PCMversion2 then exit;
  91.     BlockRead(Archivo,formato.nchannels,2);
  92.     if formato.nchannels>2 then exit;
  93.     BlockRead(Archivo,formato.nsamplespersec,4);
  94.     if formato.nsamplespersec>smCD then exit;
  95.     BlockRead(Archivo,Bytes_Por_Segundo,4);
  96.     BlockRead(Archivo,Bytes_Por_Muestra,2);
  97.     BlockRead(Archivo,formato.wbitspersample,2);
  98.     if formato.wbitspersample>rsCD then exit;
  99.     BlockRead(Archivo,tag,4);
  100.     if tag<>tgdata then exit;
  101.     BlockRead(Archivo,Tamanno_Datos,4);
  102.     if Tamanno_Datos>Tamanno_Total then exit;
  103.     //Formato:
  104.     with Formato do
  105.     begin
  106.       wFormatTag:=Wave_Format_PCM;
  107.       nblockalign:=(nchannels*wbitspersample) div 8;
  108.       nAvgBytesPerSec:=nsamplespersec*nblockalign;
  109.       cbSize:=0;
  110.     end;
  111.     result:=WV_Ok
  112.   end;
  113. begin
  114.   inherited create;
  115.   fabierto:=false;
  116.   assignfile(Archivo,FileName);
  117.   FileMode:=0;
  118.   reset(Archivo,1);
  119.   if LeerEncabezado=WV_Ok then
  120.     fabierto:=true
  121.   else
  122.     CloseFile(Archivo);
  123. end;
  124.  
  125. function TlectorWav.Leer(var Buffer; Longitud:integer):boolean;
  126. var tama,leido,totalleido:integer;
  127.     referenciaDatos:pointer;
  128. begin
  129.   if fabierto then
  130.   begin
  131.     totalleido:=0;
  132.     referenciaDatos:=@Buffer;
  133.     while totalleido<longitud do
  134.     begin
  135.       if longitud-totalleido<MaxTammanoBuffer then
  136.         tama:=longitud-totalleido
  137.       else
  138.         tama:=MaxTammanoBuffer;
  139.       blockread(archivo,referenciaDatos^,tama,leido);
  140.       inc(integer(referenciaDatos),leido);
  141.       inc(totalleido,leido);
  142.       if (tama<>leido) then
  143.         break;
  144.     end;
  145.     result:=longitud=totalleido;
  146.   end
  147.   else
  148.     result:=false;
  149. end;
  150.  
  151. destructor TlectorWav.destroy;
  152. begin
  153.   if fabierto then
  154.     CloseFile(Archivo);
  155.   inherited destroy;
  156. end;
  157.  
  158. end.
  159.  
  160.