Key tone generation method, key tone playing method and electronic equipment
By customizing the key tone parameters and sine wave generation function to generate sine wave data, the key tone file is constructed, which solves the problems of low security of key tone configuration and large storage space in the prior art, and realizes key tone generation with small storage space and high security.
Patent Information
- Application Number
- CN202411580197.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-07
- Publication Date
- 2025-02-28
AI Technical Summary
In the prior art, the configuration of key tones has problems such as low security and large storage space occupancy.
By obtaining custom key tone parameters, pre-allocating memory space, and calling the sine wave generation function to generate sine wave data, building a key tone file, realizing the generation and storage of key tone.
It effectively avoids waste of memory space, improves security, and avoids the security risks of generating or packaging and downloading through specific software.
Smart Images

Figure CN119536680A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of terminal configuration, and in particular to a key tone generation method, device and electronic equipment. Background Art
[0002] In the related art, configuring corresponding key sounds for devices with keyboards is a common way to meet customer personalized needs and remind users of key status. The key sound configuration in the related art is usually generated by special software during the device production process and then packaged and sent to the device, which has the problems of low security and large storage space occupation. Summary of the invention
[0003] The technical problem to be solved by the present invention is to provide a key tone generation method, device and electronic equipment to achieve key tone generation with small storage space and high security.
[0004] In order to solve the above technical problems, a technical solution adopted by the present invention is: A key tone generation method is applied to an electronic device, wherein the electronic device is equipped with a keyboard, and the method comprises: Get the custom key tone parameters; Allocate memory space according to the key tone parameters; Calling a sine wave generation function to generate sine wave data based on the key tone parameters, and saving the sine wave data into the memory space; A key tone file is constructed based on the sine wave data in the memory space.
[0005] In order to solve the above technical problems, another technical solution adopted by the present invention is: A key tone playing method, characterized in that it is applied to an electronic device, the method comprising: Create an audio track instance corresponding to the audio track class in the native audio framework; Obtaining a key tone file generated in the key tone generation method described above; The key tone file is stored in the ring buffer corresponding to the audio track instance, and the audio track instance is registered in the audio manager in the native audio framework; After mixing all the key tone files in the ring buffer through the audio manager, the audio hardware abstraction layer is called to output the mixed data.
[0006] In order to solve the above technical problems, another technical solution adopted by the present invention is: An electronic device comprises a memory, a processor and a computer program stored in the memory and running on the processor, wherein the processor implements each step of the above-mentioned key tone generation method when executing the computer program.
[0007] The beneficial effects of the present invention are as follows: the present application obtains custom key tone parameters, and pre-allocates memory space according to the key tone parameters, and predicts the size of the final generated audio data, i.e., the sine wave data, according to the key tone parameters, so that the problem of space waste caused by excessive allocated memory space can be avoided; at the same time, the present application calls a sine wave generation function, generates sine wave data as audio through a sine wave according to the received key tone parameters, and saves the sine wave data into a memory space, and then constructs a key tone file based on the sine wave data in the memory space. The method of the present application can be used to generate and construct the final key tone file through a sine wave generation function through custom key tone parameters, so there is no need to generate or collect through specific software, and then package and download it to an electronic device, which avoids the problem of low security in other software or packaging processes, and improves security. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] Figure 1 A flowchart of a method for generating a key tone according to an embodiment of the present invention; Figure 2 A schematic diagram of an audio framework provided by an embodiment of the present invention; Figure 3 A flowchart of a method for playing a key tone provided by an embodiment of the present invention; Figure 4 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0009] In order to make the technical problems, technical solutions and beneficial effects to be solved by the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0010] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.
[0011] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0012] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0013] In the related art, key tone can prompt the user whether the key is pressed successfully, which is an important function. In commonly used devices, key tone can usually be configured in a pre-set manner. For example, in mobile phones, computers, telephones and other devices, specific keys can be directly bound to key tone. However, for cash registers, POS terminals and other devices, there is a need for confidentiality, that is, it is necessary to avoid being able to know the specific key pressed through the key tone while being able to prompt the user through the key tone.
[0014] In the related art, key tone is an important indicator in the production, testing and use of POS terminals. Usually, different key tones are collected manually or generated by software, and then packaged and downloaded to the terminal. After the key is pressed, the key tone stored in the terminal is randomly obtained for playback. This method can be used for physical keys and virtual key tones, and can also be used for hardware testing. This method requires a dedicated storage space to store key tones, and to improve randomness, a large number of key tones need to be stored in advance. For POS terminals, the storage space is usually small, and the pre-storage method is low in cost performance. Moreover, the use of external collection and software generation methods has the risk of being tampered with or implanted with viruses during the software and download packaging process, which poses a great security risk. The related art also uses a PWM (pulse width modulation) scheme to generate key tone effects by occupying port line resources. However, this scheme has some limitations, such as the need for additional buzzer components and occupying port line resources. This is particularly obvious on POS terminals with small available space and tight resources.
[0015] In order to solve the above problems, the present application provides a method, device and electronic device for generating key tone. Figure 1 , the following specifically introduces a key tone generation method of the present application.
[0016] The key tone generation method in the present application can be used in various terminal devices that need to generate physical key or virtual key prompts, such as cash registers, POS terminals, password input machines, mobile phones, etc.
[0017] The key tone generation method of the present invention is described in detail below, including steps S110 to S140.
[0018] Step S110: Obtain customized key tone parameters.
[0019] In some embodiments, the key tone parameters include sampling rate, duration and number of channels. In this way, based on the principle that sound is essentially a wave, wave-related parameters are obtained, and related parameters required to generate an audio file are obtained, so as to facilitate the subsequent generation of random key tone.
[0020] Step S120: Allocate memory space according to the key tone parameters.
[0021] In one embodiment of the present application, step S120 includes steps S121 to S123.
[0022] Step S121, calculating the number of samples according to the sampling rate and duration.
[0023] In some embodiments, step S121 includes obtaining the sampling bit depth. In this way, a more accurate calculation parameter is provided for the memory space that needs to be reserved for subsequent calculations, so that the size of the memory space finally calculated can be close to the size of the generated key tone file, thereby ensuring that the key tone can be successfully stored and avoiding the waste of memory space to the greatest extent.
[0024] Step S122: Calculate the size of the memory space according to the number of samples and the number of channels.
[0025] In some embodiments, step S122 includes: space=numChannels×numSamples×sizeof(x); In the formula, space represents the size of the space; numChannels represents the number of channels; numSamples represents the number of samples; sizeof represents the memory capacity measurement function; x represents the data type, and sizeof(x) represents the sampling bit depth. For example, if x takes the data type short, the calculated sampling bit depth sizeof(x) = 16bit, indicating that the sampling bit depth is 16bit. The sampling bit depth indicates how many bits are used to represent each sample. In this way, the total number of final samples is obtained based on the number of channels and the number of samples provided, and the size of the space occupied by each sample is obtained based on the sampling bit depth. In this way, the prediction of the size of the space occupied by the final key tone is calculated, and the prediction of the space size is generated based on the principle of sound sampling, which improves the accuracy of the prediction result.
[0026] Step S123: Allocate memory space according to the space size.
[0027] In this way, the number of samples is obtained according to the sampling rate and duration, and the size of the memory space is calculated according to the number of samples and the number of channels. That is, before the key tone file is formally generated, the file size is first estimated and the corresponding memory space is reserved to ensure that the key tone can be generated normally and to avoid pauses due to insufficient storage space during the key tone generation process. In addition, memory space is reserved according to pre-configured key tone parameters. On the premise of ensuring that the key tone can be saved normally, storage space can also be reserved according to different key tone characteristics. Compared with the method of reserving a fixed-size memory space, it can effectively avoid wasting memory space.
[0028] Step S130, calling the sine wave generation function, generating sine wave data based on the key tone parameters, and saving the sine wave data into the memory space.
[0029] In one embodiment of the present application, before step S130, it also includes: step a, defining a sine wave generation function, including frequency, amplitude, pending parameters for receiving key tone parameters, a first process code for generating sine wave data, and a second process code for storing the sine wave data into a memory space.
[0030] In this way, the fixed frequency and amplitude parts in the sine wave generation function are pre-set, and the part for receiving the key tone parameters is reserved, so as to avoid the computational burden caused by too many random parameters while ensuring randomness. In addition, although the essence of sound waves is waves, not every waveform can become sound. The method of pre-setting the frequency and amplitude can also limit the waves to ensure that the generated sine waves can be used as sound. For example, the sound needs to have a frequency in the range of 20Hz-20000Hz to be heard by the human ear, where Hz represents Hertz. At the same time, the first process code is pre-set for generating sine wave data, and the second process code is pre-set for storing the sine wave data in the memory space. Then, the generation and storage of sine wave data can be automatically realized by calling the sine wave generation function.
[0031] Step S140: construct a key tone file based on the sine wave data in the memory space.
[0032] In one embodiment of the present application, before step S140, the following steps are included: step b, defining a file header of the touch tone file.
[0033] Then step S140 includes: step c, filling the file header according to the sine wave data, and constructing a key tone file according to the filled file header and the sine wave data.
[0034] In this way, the format of the key tone file is predefined, and after the sine wave data is obtained, the format of the key tone file can be directly filled in to generate the key tone, which improves the efficiency of generating the key tone file. The file header is also predetermined, which makes it convenient to obtain the file information according to the file header after the key tone file is generated. If only the attribute information of the key tone file needs to be obtained, there is no need to decode the entire key tone file each time, which improves the efficiency of subsequent file parsing.
[0035] In one embodiment of the present application, after step S140, the following is included: step d, after calling the audio track class in the native audio framework to play the key tone file, the memory space is released. In this way, the key tone file is deleted immediately after use, and the key tone is randomly generated again when the key tone needs to be played next time, which ensures the randomness of the key tone and improves the security of the system. And because each key tone is randomly generated, there is no need to keep the key tone in the storage space, which further reduces the storage space occupied and saves storage resources.
[0036] To summarize, the scheme of the present application pre-configures a sine wave generation function, which can generate sine wave data based on the key tone parameters and save it in the memory space, and finally constructs a playable key tone file based on the sine wave data in the memory space. Moreover, since the key tone is generated in real time, the corresponding memory space can be released immediately after the key tone file is played, and the storage space will not be occupied when the key tone is not needed. There is no need to store the complete audio file for playback, but when the key tone is needed, the corresponding sine wave data is generated by the sine wave generation function according to the principle that sound is a wave to construct a playable key tone file, which saves storage space. The function of generating sine wave data does not occupy port line resources, thereby reducing the hardware requirements of the real-time key tone generation function.
[0037] Moreover, the key tone parameters are received from external input or randomly generated, and different key tone parameters can be configured for each key tone generation, thereby achieving random key tone. While meeting the need to prompt the key to be pressed, it avoids the association between the key tone and the specific key from being leaked, thereby improving confidentiality.
[0038] Before generating the key tone file, pre-allocate memory space according to the key tone parameters obtained, that is, reserve enough space for the key tone file generation to avoid the failure of key tone generation due to insufficient memory. Fix some parameters in the sine wave generation function to ensure that the final generated wave can be used as a sound wave, avoiding the situation where the final generated sine wave data cannot be converted into audio.
[0039] The following specifically introduces an application embodiment of the present application. The present application can apply the above solution in the Android system framework, including steps (1)-(7).
[0040] (1) Define the WAV file header structure WavHeader, which is used to store the relevant information of the WAV file. The WAV file is the key tone file mentioned above. This step is equivalent to the above step b.
[0041] (2) Define the sine wave generation function generateSineWave; this function accepts parameters such as frequency, amplitude, duration, sampling rate, buffer and number of channels, and stores the generated sine wave in the buffer, which is the area in the memory space mentioned above. The frequency, amplitude and buffer can be fixed values. If they need to be changed, the corresponding constants or buffer addresses can be directly modified; the duration and sampling rate can be received from the user when a key tone needs to be generated, that is, each time a key tone is generated, an input duration and sampling rate are corresponding. This step is equivalent to step a above.
[0042] (3) Set the custom sampling rate customSampleRate, the custom duration customDuration and the number of channels numChannels. This step is equivalent to the above step S110.
[0043] (4) Calculate the number of samples numSamples generated according to customSampleRate and customDuration, and allocate a memory space of numChannels * numSamples * sizeof(short), that is, as a buffer audioBuffer for storing audio data. This step is equivalent to the above step S120.
[0044] (5) Call the generateSineWave function to generate a sine wave of the specified frequency and amplitude, and store the result in audioBuffer. This step is equivalent to the above step S130.
[0045] (6) Construct the WAV file header WavHeader, set the value of each field according to the audio generated by the generateSineWave function. Write the WAV file header information and audio data into the key tone file. This step is equivalent to the above step S140.
[0046] (7) Use the Android audio framework to play the audio. After the playback is finished, close the file, release the memory, and end the program. This step is equivalent to step d above.
[0047] Because the key tone generation method in this application is used, the key tone file is generated in real time when it needs to be played and released immediately after the playback ends, so the overall size requirement of the key tone file can be reduced. In actual applications, you can choose to use a high-fidelity audio format such as WAV format to further ensure the quality of audio playback. And all the functions and parameters used are built into the system, which is convenient for multiple calls to the corresponding functions and parameters to generate key tone files.
[0048] The present invention also provides a key tone playing method, comprising steps S210 to S240.
[0049] Step S210: Create an audio track instance corresponding to the audio track class in the native audio framework.
[0050] Step S220: Acquire a key tone file generated by the key tone generation method described above.
[0051] Step S230: store the key tone file into the ring buffer corresponding to the audio track instance, and register the audio track instance in the audio manager in the native audio framework.
[0052] Step S240: After mixing all the key tone files in the ring buffer through the audio manager, the audio hardware abstraction layer is called to output the mixed data.
[0053] In one embodiment of the present application, the above-mentioned method for playing a key tone further includes creating an audio playback interface, and after receiving an interface trigger signal, the audio playback interface creates an audio track instance corresponding to the audio track class in the native audio framework. The audio playback interface is created to be externally unified, and after the audio playback interface receives an interface trigger signal, a corresponding audio track instance is created to be responsible for the execution of a specific playback process.
[0054] In one embodiment of the present application, the audio playback interface is directly called during testing, and the audio playback interface receives an interface trigger signal; or when a physical key / virtual key is pressed, an interface trigger signal is sent to the audio playback interface.
[0055] According to the steps in S210-S240, the generated key tone file is directly mixed and played through the native audio framework without the need for additional audio framework modification; and the native framework can create multiple parallel audio track instances according to the initiated audio playback request, and finally mix through the audio manager, and can independently play multiple audios, solving the problem of mixing. The specific playback process has been introduced in detail in the Android framework of the relevant technology, and will not be repeated here.
[0056] Please refer to Figure 3 The following is a detailed description of the application embodiment of the present application. The present application can apply the above-mentioned key tone playing method in the Android system framework, including steps ①-④ ① When the application uses the Android audio framework to play the language, it creates a thread at the AudioTrack (audio track class) layer. Create an external audio playback interface, and create a corresponding thread after the interface is triggered. This step is equivalent to the above step S210.
[0057] Provides a unified audio playback interface for external devices such as physical buttons and virtual button sounds of POS terminals (virtual buttons refer to buttons drawn on the LCD display, which ultimately call the same button sound interface as physical buttons). It can also be used to provide hardware test sound interfaces (no separate permissions and interfaces are required, AudioFlinger supports mixing button sounds played by different processes, so there is no need to consider the problem of simultaneous calls) to meet the testing needs of hardware personnel, such as testing the output power, frequency response, distortion, power ripple, operating current, amplifier timing, etc. of button sounds.
[0058] ② After the external audio interface receives the key tone parameters to be played, the interface is triggered, and the interface generates a key tone file through the above key tone generation method and stores it in the memory space buffer. This step is equivalent to the above step S220.
[0059] ③ Finally, the key tone file data will be written into the ring buffer, and the audio track instance will be registered in the audio manager AudioFlinger. This step is equivalent to the above step S230.
[0060] ④Please refer to Figure 2 After mixing (MixerThread) all the key tone files in the wake-up buffer through the audio manager AudioFlinger, the audio hardware abstraction layer AudioHAL is called to output the mixed data (primary_out). This step is equivalent to the above step S240.
[0061] Please refer to Figure 4 The present invention also provides an electronic device 300, including a memory 301 and a processor 302, and a computer program stored in the memory 301 and running on the processor 302. When the processor 302 executes the computer program, the various steps in the above-mentioned key tone generation method are implemented.
[0062] The electronic device of the present application can also be used to implement the above-mentioned key tone playing method.
[0063] The beneficial effects of the electronic device of the present invention are the same as those of the above method and will not be described in detail here.
[0064] The above are only embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent transformations made using the contents of the specification and drawings of the present invention, or directly or indirectly applied in related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for generating a key tone, characterized in that: Applied to electronic equipment, the method comprises: Get the custom key tone parameters; Allocate memory space according to the key tone parameters; Calling a sine wave generation function to generate sine wave data based on the key tone parameters, and saving the sine wave data into the memory space; A key tone file is constructed based on the sine wave data in the memory space.
2. A method for generating a key tone according to claim 1, characterized in that: The key tone parameters include sampling rate, duration and number of channels; The allocating memory space according to the key tone parameters comprises: Calculate the number of samples according to the sampling rate and duration; Calculate the size of the memory space according to the number of samples and the number of channels; Allocate memory space according to the space size.
3. A method for generating a key tone according to claim 2, characterized in that: It also includes sampling bit depth; Calculating the size of the memory space according to the number of samples and the number of channels includes: space=numChannels×numSamples×sizeof(x); In the formula, space represents the size of the space; numChannels represents the number of channels; numSamples represents the number of samples; sizeof represents a memory capacity measurement function; x represents a data type; sizeof(x) represents the sampling bit depth.
4. A method for generating a key tone according to claim 1, characterized in that: The calling of the sine wave generating function includes: A sine wave generating function is defined, including frequency, amplitude, pending parameters for receiving the key tone parameters, a first process code for generating sine wave data, and a second process code for storing the sine wave data into a memory space.
5. A method for generating a key tone according to claim 1, characterized in that: The step of constructing a key tone file based on the sine wave data in the memory space includes: Define the file header of the key tone file; The step of constructing a key tone file based on the sine wave data in the memory space comprises: The file header is filled according to the sine wave data, and a key tone file is constructed according to the filled file header and the sine wave data.
6. A method for generating a key tone according to claim 1, characterized in that: The step of constructing a key tone file based on the sine wave data in the memory space includes: After calling the audio track class in the native audio framework to play the key tone file, the memory space is released.
7. A method for playing a key tone, characterized in that: Applied to electronic equipment, the method comprises: Create an audio track instance corresponding to the audio track class in the native audio framework; Obtain the key tone file according to any one of claims 1 to 6; The key tone file is stored in the ring buffer corresponding to the audio track instance, and the audio track instance is registered in the audio manager in the native audio framework; After mixing all the key tone files in the ring buffer through the audio manager, the audio hardware abstraction layer is called to output the mixed data.
8. A key tone playing method according to claim 7, characterized in that: The step of creating an audio track instance corresponding to the audio track class in the native audio framework includes: After receiving the interface trigger signal, create an audio track instance corresponding to the audio track class in the native audio framework.
9. A key tone playing method according to claim 8, characterized in that: The method further comprises: An audio playback interface is created, where the audio playback interface is used to create an audio track instance corresponding to an audio track class in a native audio framework after receiving an interface trigger signal.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, each step of the method for generating a key tone according to any one of claims 1 to 6 is implemented.