rawaudio.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:4k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: rawaudio.c 543 2006-01-07 22:06:24Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include "common.h"
  24. int RawAudioInit(rawaudio* p)
  25. {
  26. format_stream* s = Format_AddStream(&p->Format,sizeof(format_stream));
  27. p->Head = 0;
  28. if (s)
  29. {
  30. PacketFormatClear(&s->Format);
  31. s->Format.Type = PACKET_AUDIO;
  32. s->Format.Format.Audio.Format = p->Type;
  33. s->PacketBurst = 1;
  34. s->Fragmented = 1;
  35. s->DisableDrop = 1;
  36. Format_PrepairStream(&p->Format,s);
  37. if (p->Format.Comment.Node)
  38. {
  39. format_reader* Reader = p->Format.Reader;
  40. char Head[ID3V2_QUERYSIZE];
  41. // id3v1
  42. char Buffer[ID3V1_SIZE];
  43. filepos_t Save = Reader->Input->Seek(Reader->Input,0,SEEK_CUR);
  44. if (Save>=0 && Reader->Input->Seek(Reader->Input,-(int)sizeof(Buffer),SEEK_END)>=0)
  45. {
  46. if (Reader->Input->Read(Reader->Input,Buffer,sizeof(Buffer)) == sizeof(Buffer))
  47. ID3v1_Parse(Buffer,&p->Format.Comment);
  48. Reader->Input->Seek(Reader->Input,Save,SEEK_SET);
  49. }
  50. // id3v2
  51. if (Reader->Read(Reader,Head,sizeof(Head))==sizeof(Head))
  52. {
  53. int TagSize = ID3v2_Query(Head,sizeof(Head));
  54. if (TagSize > 0)
  55. {
  56. char* Buffer = (char*)malloc(TagSize);
  57. if (Buffer)
  58. {
  59. int Len;
  60. memcpy(Buffer,Head,sizeof(Head));
  61. Len = Reader->Read(Reader,Buffer+sizeof(Head),TagSize-sizeof(Head));
  62. if (Len>=0)
  63. ID3v2_Parse(Buffer,Len+sizeof(Head),&p->Format.Comment,0);
  64. free(Buffer);
  65. }
  66. p->Head = TagSize;
  67. }
  68. }
  69. Reader->Seek(Reader,p->Head,SEEK_SET);
  70. }
  71. }
  72. p->SeekPos = p->Head;
  73. return ERR_NONE;
  74. }
  75. static int ReadPacket(rawaudio* p, format_reader* Reader, format_packet* Packet)
  76. {
  77. format_stream* Stream = p->Format.Streams[0];
  78. if (Reader->FilePos<=p->Head)
  79. Packet->RefTime = 0;
  80. else
  81. if (Stream->Format.ByteRate && !p->VBR)
  82. Packet->RefTime = Scale(Reader->FilePos-p->Head,TICKSPERSEC,Stream->Format.ByteRate);
  83. else
  84. if (Reader->FilePos<=p->SeekPos)
  85. Packet->RefTime = p->SeekTime;
  86. else
  87. Packet->RefTime = TIME_UNKNOWN;
  88. Packet->Data = Reader->ReadAsRef(Reader,-4096); //processing by 4K blocks are enough
  89. Packet->Stream = Stream;
  90. if (Stream->LastTime < TIME_UNKNOWN)
  91. Stream->LastTime = TIME_UNKNOWN;
  92. return ERR_NONE;
  93. }
  94. int RawAudioSeek(rawaudio* p, tick_t Time, filepos_t FilePos,bool_t PrevKey)
  95. {
  96. format_stream* Stream = p->Format.Streams[0];
  97. if (FilePos < 0)
  98. {
  99. if (Time > 0)
  100. {
  101. if (Stream->Format.ByteRate)
  102. FilePos = p->Head + Scale(Time,Stream->Format.ByteRate,TICKSPERSEC);
  103. else
  104. return ERR_NOT_SUPPORTED;
  105. }
  106. else
  107. FilePos = p->Head;
  108. }
  109. p->SeekTime = Time>0?Time:TIME_UNKNOWN;
  110. p->SeekPos = FilePos;
  111. return Format_Seek(&p->Format,FilePos,SEEK_SET);
  112. }
  113. static int Create(rawaudio* p)
  114. {
  115. if (stscanf(LangStr(p->Format.Format.Class,RAWAUDIO_FORMAT),T("acodec/0x%x"),&p->Type)!=1)
  116. return ERR_INVALID_PARAM;
  117. p->Format.Init = (fmtfunc)RawAudioInit;
  118. p->Format.Seek = (fmtseek)RawAudioSeek;
  119. p->Format.ReadPacket = (fmtreadpacket)ReadPacket;
  120. p->SeekTime = 1; // but don't want to return 0
  121. p->VBR = 1;
  122. return ERR_NONE;
  123. }
  124. static const nodedef RawAudio = 
  125. {
  126. sizeof(rawaudio)|CF_ABSTRACT,
  127. RAWAUDIO_CLASS,
  128. FORMATBASE_CLASS,
  129. PRI_DEFAULT,
  130. (nodecreate)Create,
  131. NULL,
  132. };
  133. void RawAudio_Init()
  134. {
  135.  NodeRegisterClass(&RawAudio);
  136. }
  137. void RawAudio_Done()
  138. {
  139. NodeUnRegisterClass(RAWAUDIO_CLASS);
  140. }