⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aflibmemcache.cc

📁 一个共享源码的音频库2
💻 CC
📖 第 1 页 / 共 2 页
字号:
            // IF end point of node is before or equal new data end point            if (node_position + node_size <= position + size)            {               // Case 1               processed = TRUE;               vector<int>& array = (*it).second->getData();               for (i = 0; i < node_size + node_position - position; i++)               {                  for (j = 0; j < chans; j++)                  {                     array[i * chans + j + position - node_position] =                        data.getSample(i + position - orig_position, j);                  }               }               // Update node information               size -= node_size + node_position - position;               position = node_size + node_position;            }            else            {               // Case 2               processed = TRUE;               vector<int>& array = (*it).second->getData();               for (i = 0; i < size; i++)               {                  for (j = 0; j < chans; j++)                  {                     array[i * chans + j + position - node_position] =                        data.getSample(i + position - orig_position, j);                  }               }               break;            }         }         // ELSE beginning point of node is after new data start AND         // before end of new data         else if (node_position < position + size)         {            // Case 3            processed = TRUE;            // IF there is data before this node then add it as new node            if (node_position > position)            {               createNewNode(0, node_position - position, position, data);               // Update node information               size -= (node_position - position);               position = node_position;            }            vector<int>& array = (*it).second->getData();            // Replace the rest of data in existing node            // Check to see if new data ends before node data ends            if (size + position <= node_position + node_size)            {               for (i = 0; i < size; i++)               {                  for (j = 0; j < chans; j++)                  {                     array[i * chans + j] =                        data.getSample(i + position - orig_position, j);                  }               }               break;            }            else            {               for (i = 0; i < node_size; i++)               {                  for (j = 0; j < chans; j++)                  {                     array[i * chans + j] =                        data.getSample(i + position - orig_position, j);                  }               }               // Update node information               size -= node_size;               position += node_size;            }         }         else         {            // IF we get here the node is completely past the new data and we are done            break;         }      }   }   return (processed);}voidaflibMemCache::createNewNode(   int start_element,   int stop_element,   long long position,   aflibData& data){   // This private function adds a new node to the node array that stores memory data   aflibMemNode  * node;   int i, j;   long long  size;   int  chans;   chans = data.getConfig().getChannels();   // Create new memory node    node = new aflibMemNode();   node->setChannels(chans);   vector<int>& array = node->getData();   // Loop over samples then channels   for (i = start_element; i < stop_element; i++)   {      for (j = 0; j < chans; j++)      {         array.push_back(data.getSample(i, j));      }   }    // Since everything stored as ints use 4 bytes   size = ((stop_element - start_element) * node->getChannels() * 4);   _cache_size_local += size;   _cache_size_total += size;    // Store new memory node into set   _node_array[position] = node;}voidaflibMemCache::fillDataFromCache(   aflibData& data,   long long& position,   int& num_samples,   long long orig_position,   int orig_num_samples){   // This function will goto the cache fill in any necessary data that is missing from the   // the start of the data. It does not support filling in data from any other   // location. If we fail to fill in any data it is an error in another function in this   // class that said we could. Parameters position and num_samples indicate new data that   // was not cached but was obtained from the audio chain. Parameters orig_position and   // orig_num_samples indicate the original data that was asked for whether data was   // cached or not. Thus position - orig_position indicates data that was cached and will   // be retrieved by this function.   //   //    ---------------------------------------------------------------------------   //   |                       |                                                   |   //   |                       |                                                   |   //   |<----------A---------->|<-----------------------B------------------------->|   //   |                       |<-----------------num_samples--------------------->|   //   |<---------------orig_num_samples------------------------------------------>|   //    ---------------------------------------------------------------------------   //   ^orig_position          ^position   //   //   Above shows the data layout. data when this function is called contains B. The   //   A data will be obtained from the cache. When this function exits it will contain   //   A + B.   aflibData  * new_data = NULL;   aflibData  * data_ptr = &data;   map< long long, aflibMemNode* >::iterator it;   long long n_position;   long long n_size;   int  i, j;   int  chans;	if(position == -1) return;   // Cache audio data if needed   cacheData(position, data);   chans = data.getConfig().getChannels();   // IF we need more memory allocate a new data array   if (orig_num_samples > data.getOrigLength())   {      new_data = new aflibData(data.getConfig(), orig_num_samples);      data_ptr = new_data;   }   // IF we need data from the start then get the data   if (position != orig_position)   {      // move data from current position to make room for new data at start      for (i = 0; i < num_samples; i++)      {         for (j = 0; j < chans; j++)         {            data_ptr->setSample(data.getSample(i, j), i + position - orig_position, j);         }      }      // Now insert the data at the start      // Loop thru all nodes       for (it = _node_array.begin(); it != _node_array.end(); it++)      {         n_position = (*it).first;         n_size = (*it).second->getSize();         // Are we done         if (n_position >= position)         {            break;         }         // Does this node contain data for us         // TBD may need to check if next node has data for us as well         if ((orig_position >= n_position) && (orig_position <= n_position + n_size))         {            int local_size;            int local_offset;            // Find offset into data            local_offset = (int)(orig_position - n_position);            // We need to find the size of the data that is available for us            if ((n_position + n_size - orig_position) >= (position - orig_position))            {               // All data is in node use it               local_size = position - orig_position;            }            else            {               // Only use amount available from node               local_size = n_position + n_size - orig_position;            }            // Fill in the data            for (i = 0; i < local_size; i++)            {               for (j = 0; j < chans; j++)               {                  data_ptr->setSample(                    (*it).second->getData()[(i + local_offset) * chans + j],                    i, j);               }            }            num_samples += local_size;            position -= local_size;         }      }   }#if 0   // We should never need this case for this algorithm   // IF we need data at the end then get the data   if (num_samples != orig_num_samples)   {      cerr << "NOT YET IMPLEMENTED!" << endl;   }#endif   // IF we allocated a new data object then copy it to the one returned   if (new_data)   {      data = *new_data;      delete new_data;   }}/*! \brief Empties the cache for this object only.*/voidaflibMemCache::clearCache(){   map< long long, aflibMemNode* >::iterator it;   // Delete all allocated objects   for (it = _node_array.begin(); it != _node_array.end(); it++)   {      delete (*it).second;   }   // Erase all nodes   if (_node_array.size() != 0)   {      _node_array.erase( _node_array.begin(), _node_array.end());   }   _cache_size_total -= _cache_size_local;   _cache_size_local = 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -